What Every .htaccess File Must Have

Here is a list of things you should have in your .htaccess file

Prevent Bandwidth Leaching

If somebody wants to put an image from your site in theirs they should at least have the decency to download it and uploaded to their server, unfortunately some people don’t and instead just use the link to your image.

You can get back at them by showing any image you want in the place of the image they are linking to.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://yourdomain.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com/ [NC]
RewriteCond %{REQUEST_URI} !^/stolen.gif [NC]
RewriteRule \.(gif|GIF|jpg|JPG)$ http://yourdomain.com/allowed/stolen.gif [R]

source: http://www.wiscocomputing.com/articles/using_htaccess.htm

Preferred URL

How do you want your site’s URL to look in the google search results? with our without the www?

To remove the www use:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

to have the www use:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301]

Compression

<IfModule mod_deflate.c>
  Header add X-Enabled mod_deflate  
</IfModule>

<IfModule mod_gzip.c>
  Header add X-Enabled mod_gzip  
</IfModule>

Your Own Error Documents

Why greet your visitors with the default not found page your sever provides when your can design your own that is likely to keep visitors. Make a folder called errors and add the following html files to it.

ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/serverr.html

source: http://www.javascriptkit.com/howto/htaccess2.shtml for a full list of errors.

Block Access To .htaccess file

Use this if you are hiding something in the .htaccess file

<Files .htaccess>
order allow,deny
deny from all
</Files>

source: http://www.wiscocomputing.com/articles/using_htaccess.htm

blog comments powered by Disqus