Filter broken referer Django emails when switching servers
Question: We often change IP addresses on AWS when switching EC2 servers (yes, we use
Elastic IPs when applicable). Sometimes we get IP addresses that used to host
other applications (which is fine, of course). When end users click on stale
links they get to our servers (which is ok). But these HTTP GET requests have
a "referer" header, thus the regular 404 error generates an automated Django
email to our developers!

I can recreate the error emails easily using the following Python code:

    
    
    import urllib2
    req = urllib2.Request('http://my_website.com/some/url/we/dont/have')
    req.add_header('Referer', 'http://whatever.i.want')
    request = urllib2.urlopen(req)
    

When the Referer header is commented out Django does not send emails.  
We still want the emails to be sent when there are real broken links in OUR
website, so I do not want to set `SEND_BROKEN_LINK_EMAILS` to False.  
I cannot filter using `IGNORABLE_404_URLS` because a. the logic is reversed
and b. the regular expression only scans the path and not the hostname.

Help?


Answer: I'd probably knock up some
[middleware](https://docs.djangoproject.com/en/dev/topics/http/middleware/?from=olddocs#process-
exception) that catches the `Http404` exception, examines the referer header,
and either raises a separate exception (probably will still be mailed though),
or does a simple redirect to your front page. Even better, create a custom
page describing why the user ended up where they are, and just redirect to
that.

