Mod_Rewrite

mod_rewrite is an apache module for rewriting URL’s

The most common use of this is in a .htaccess file, however it can be placed in the httpd.conf/apache.conf.

Mod_rewrite works on the premise of rule > action

So we could have a rule that matches all requests for a particular file e.g. mypic.jpg and forwards requests for that file to another file e.g. notallowed.jpg.

A common use of the mod_rewrite is to stop hot-linking, by specifying a rule that matches say all pictures if the request is from a remote server, and forwarding that request to a page saying that hot linking is not allowed. If the request is from the local server, the rule will not be matched, and the server will show the correct image.

An example of how to do this is below:

RewriteCond %{HTTP_REFERER} ^(.+\.)?mysite1.com/ [OR]
RewriteCond %{HTTP_REFERER} ^(.+\.)?mysite2.com/  [OR]
RewriteCond %{HTTP_REFERER} ^(.+\.)?mysite3.com/
RewriteCond %{REQUEST_URI} !/path/to/your/nohotlink.png
RewriteRule .*\.(gif|jpg|png|avi)$http://yoursite.com/path/to/your/nohotlink.png

The rules above will check the referrer id to see if the request comes from mysite1.com, mysite2.com or mysite3.com. If it does, and the request isn’t for nohotlink.png (the file we’re showing to hotlinkers) then we’ll redirect the request to show the file nohotlink.png.

More information about mod_rewrite can be found at http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>