Monday, August 9, 2010

htaccess Cheat Sheet


Enable Directory Browsing
bc.. Options +Indexes
1. block a few types of files from showing
IndexIgnore *.wmv *.mp4 *.avi
Disable Directory Browsing
bc.. Options All -Indexes
Customize Error Messages
bc.. ErrorDocument 403 /forbidden.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /servererror.html
Get SSI working with HTML/SHTML
bc.. AddType text/html .html
AddType text/html .shtml
AddHandler server-parsed .html
AddHandler server-parsed .shtml
1. AddHandler server-parsed .htm
Change Default Page (order is followed!)
bc.. DirectoryIndex myhome.htm index.htm index.php
Block Users from accessing the site
bc..
order deny,allow
deny from 202.54.122.33
deny from 8.70.44.53
deny from .spammers.com
allow from all
Allow only LAN users
bc.. order deny,allow
deny from all
allow from 192.168.0.0/24
Redirect Visitors to New Page/Directory
Block site from specific referrers
bc.. RewriteEngine on
RewriteCond %{HTTP_REFERER} site-to-block\.com [NC]
RewriteCond %{HTTP_REFERER} site-to-block-2\.com [NC]
RewriteRule .* – [F]
Block Hot Linking/Bandwidth hogging
bc.. RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ – [F]
Want to show a “Stealing is Bad” message too?
Add this below the Hot Link Blocking code:
bc. RewriteRule \.(gif|jpg)$ http://www.mydomain.com/dontsteal.gif[R,L]
Stop .htaccess (or any other file) from being viewed
bc..
order allow,deny
deny from all
Avoid the 500 Error
bc.. # Avoid 500 error by passing charset
AddDefaultCharset utf-8
Grant CGI Access in a directory
bc.. Options +ExecCGI
AddHandler cgi-script cgi pl
1. To enable all scripts in a directory use the following
2. SetHandler cgi-script
Password Protecting Directories
Use the .htaccess Password Generator and follow the brief instructions!
Change Script Extensions
bc. AddType application/x-httpd-php .gne
gne will now be treated as PHP files! Similarly, x-httpd-cgi for CGI files, etc.
Use MD5 Digests
Performance may take a hit but if thats not a problem, this is a nice option to turn on.
bc. ContentDigest On
The CheckSpelling Directive
From Jens Meiert: CheckSpelling corrects simple spelling errors (for example, if someone forgets a letter or if any character is just wrong). Just add CheckSpelling On to your htaccess file.
The ContentDigest Directive
As the Apache core features documentation says: “This directive enables the generation of Content-MD5 headers as defined in RFC1864 respectively RFC2068. The Content-MD5 header provides an end-to-end message integrity check (MIC) of the entity-body. A proxy or client may check this header for detecting accidental modification of the entity-body in transit.
Note that this can cause performance problems on your server since the message digest is computed on every request (the values are not cached). Content-MD5 is only sent for documents served by the core, and not by any module. For example, SSI documents, output from CGI scripts, and byte range responses do not have this header.”
To turn this on, just add ContentDigest On.
Save Bandwidth
bc.. # Only if you use PHP
php_value zlib.output_compression 16386
Turn off magic_quotes_gpc
bc.. # Only if you use PHP
php_flag magic_quotes_gpc off