Monday, May 12th, 2008
Hotlinking is the practice of linking to an object (generally an image file) from one site (the donor site) onto another (the linking site). We’re frequently asked by our hosting clients if there is a way to stop people doing this as it’s consuming their bandwidth or server resources. This is easily done with the mod_rewrite rules below.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?opensourcery.co.uk/.*$ [NC]
RewriteRule \.(gif|png|jpe?g|$ - [F]
This will deny requests for any .gif, .png, .jpg or .jpeg file unless the referrer is either www.opensourcery.co.uk or opensourcery.co.uk.
You can substitute the last line for the one below if you wish to redirect people to an alternative image rather than simply denying the requests.
#RewriteRule \.(gif|png|jpe?g|$ img/dontsteal.gif [L,NC]
It’s worth remembering that the referrer header is optional so although almost every modern browser sends this with each request it’s possible that some won’t do meaning these rewrite rules may result in broken images for some users of your site.
Posted in Apache, Tips and tricks | No Comments »
Sunday, April 13th, 2008
A public sector customer recently had a requirement to redirect visitors to one of their applications to a holding page overnight while the company who support the application carried out some maintenance work. The work was being carried out between 1am and 7am to minimize disruption to the members of public who use the application.
Keen to help, but also to get a good nights sleep we looked to mod_rewrite to provide a solution (we manage the front end servers, which run Apache).
We added the following lines to our httpd.conf to redirect any traffic to the site to a holding page at /static/offline.php from 00:55 until 07:30 to a holding page:
RewriteEngine on
RewriteCond %{TIME} >20080407055000
RewriteCond %{TIME} <20080407073000
RewriteRule !^/static/offline.php$ /static/offline.php [PT,NS]
Alias /static "/var/www/www.example.com/static"
As the application sits on a Tomcat instance behind mod_proxy, we also added the following line to prevent mod_proxy from handling requests for the /static directory.
ProxyPass /static !
We created the offline.php file, including with the line below to ensure that search engines didn’t index the holding page.
header("HTTP/1.1 503 Service Temporarily Unavailable")
Posted in Apache, Tips and tricks | No Comments »