Skip to Content
Apache Cookbook, 2nd Edition
book

Apache Cookbook, 2nd Edition

by Rich Bowen, Ken Coar
December 2007
Beginner to intermediate
310 pages
8h 8m
English
O'Reilly Media, Inc.
Content preview from Apache Cookbook, 2nd Edition

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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Linux Cookbook, 2nd Edition

Linux Cookbook, 2nd Edition

Carla Schroder
Kubernetes Cookbook

Kubernetes Cookbook

Sébastien Goasguen, Michael Hausenblas

Publisher Resources

ISBN: 9780596529949Errata Page