5.14. Redirecting Unreferred Requests to an Explanation Page
Problem
When a request comes to your server without a referring URL being mentioned, you want to redirect it to a page that explains why you’re not satisfying it.
Solution
Add lines such as the following to your configuration files:
RewriteEngine On
RewriteCond "%{HTTP_REFERER}" "^$"
RewriteRule "(.*)" "/cgi-bin/need-referer" [PT,E=ORIG:$1]Tip
Be sure to put these directives in an appropriate scope, or people won’t be able to type in your site’s URLs at all!
Discussion
The specific problem being addressed in the Solution is requests
coming to your server with no information about where they got the URL
(which is passed in the Referer
request header field). This frequently indicates someone surfing
directly to the page rather than following a link, and in the case of
images informaltable may be the result of a “Save As” request on the
user’s part. If you want to only serve requests that came from links
on your own site, you can change the RewriteCond directive to:
RewriteCond "%{HTTP_REFERER}" "!http://%{SERVER_NAME}/" [NC]The RewriteRule will pass the
request to a CGI script named need-referer, and will put the original URI
into the environment variable ORIG
for the script to reference. Note that only the local portion of the
URI is included; the hostname will not be, for instance.
Warning
The lack of a Referer request header field does not always signify skulduggery. Some people consider it none of a site’s business how they came ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access