This .htaccess is continuation to my previous blog post(Using .htaccess for day to day work), which recorded a lot more hits than expected. I would enlist some other tricks/function we can achieve using this magic .htaccess file. For those who are reading this tutorial for the first time or are new to .htaccess are suggested to read the main tutorial to know what .htaccess is ?

Handling Document process errors

There are many times, when a bad request is sent to the server by the client and user is sent 404 page not found code as a response. Being a webmaster, you would like to retain your user on your site and show him the correct way by suggesting him search page or a customized error page. You can redirect your user to different pages according to different error codes. For example the following code

ErrorDocument 404 /errors/custom_error.html

Adding Mime Types to your server

There are times when your server doesnt support certain mime types. This happens especially in case of mp3,swf etc. You can do so by adding this line to ur .htaccess file.

AddType application/x-shockwave-flash swf

AddType is specifying that you are adding a MIME type. The application string is the actual parameter of the MIME you are adding, and the final little bit is the default extension for the MIME type you just added, in our example this is swf for ShockWave File.

There is a neat trick here, to force a file to be downloaded, via the Save As browser feature. You can simply set a MIME type to application/octet-stream and that immediately prompts you for the download.

Hot Link Protection

Protecting bandwidth of your server and resources like images is a real pain at times. There are lots of sites which link up images used on your site to theirs. To protect against such theft, you put the following code into your .htaccess. You need to change to your actual domain.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?*$ [NC]
RewriteRule \.(gif|jpg|js|css)$ - [F]

Put this code into .htaccess present at root or any subdirectory. This will protect the images of type gif and jpg where as javascript files and CSS as well.

Will write up few more tweaks, keep reading

Cheers !!

Realin !

Share this post: