htaccess Cheatsheet
apache, htaccess, redirectsAs a web developer we all have all used our htaccess file for one thing or another. However, many developers are not aware of everything an htaccess file can do for them. I have provided the following cheatsheet as a reference for a handful of these functions.
If you have some additional tips that should be added to this list, please feel free to post them in the comments and I will add them to the resource.
Enable Directory Browsing
Options +Indexes
## block a few types of files from showing
IndexIgnore *.wmv *.mp4 *.avi
Disable Directory Browsing
Options All -Indexes
Custom Error Message Pages
ErrorDocument 403 /somePage1.html
ErrorDocument 404 /somePage2.html
ErrorDocument 500 /somePage3.html
Change Default Page Order
DirectoryIndex myhome.htm index.htm index.php
Ban User IPs
order deny,allow
deny from 123.456.789.00
deny from 123.456.789.00
deny from .someDomain.com
allow from all
Block Visitors From Specific Referring Sites
RewriteEngine on
RewriteCond %{HTTP_REFERER} site-to-block.com [NC]
RewriteCond %{HTTP_REFERER} site-to-block-2.com [NC]
RewriteRule .* - [F]
Block Specific Search Spiders From Site
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^searchbot1 [OR]
RewriteCond %{HTTP_USER_AGENT} ^searchbot2 [OR]
RewriteCond %{HTTP_USER_AGENT} ^searchbot3
RewriteRule ^(.*)$ http://www.yourSite.com/goAway.htmlOR
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^searchbot1 [OR]
RewriteCond %{HTTP_USER_AGENT} ^searchbot2 [OR]
RewriteCond %{HTTP_USER_AGENT} ^searchbot3
RewriteRule ^(.*)$ http://www.someOtherWebsite.com/ [R, L]
I have also written a post on the use of 301 Redirects for SEO
It appears you are new around here. You may want to subscribe to my RSS feed or register for email updates. Thanks for visiting!
Related Posts






















November 2, 2008 @ 9:05 am
The last two items, “Block Search Spiders From Specific Directories” and “Block Specific Search Spiders From Site” will not work in a “.htaccess” file because they are used in “robots.txt” files instead. If it would helpful to proofread your article before publishing it.
[Reply]
zacheos
Bill - Thank you for pointing out my error. I have made corrections to post above and provided a better method of blocking bad robots through the htaccess file.
[Reply]
Bill
zacheos, you do need to read the Apache documentation. Your last example (”Block Specific Search Spiders From Site”) after your editing won’t work either. When redirecting to an external site, you must include the “R” flag in the RewriteRule directive in Apache 1.3 and 2.2 (Reference: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule). The documentation also states to include the “L” flag so that rewrite processing stops with this rule.
The fifth line should be:
RewriteRule ^(.*)$ http://www.someWebsite.com/ [R, L]
[Reply]
zacheos
Bill - Thanks for the clarification. I was not thinking about a redirection to another site, but rather to a trap page within the domain. I should have been more clear. I have updated the above post with clarified language. Thanks.
November 2, 2008 @ 9:35 am
[...] in part from Zach Graeves blog, there’s also a good post on 301 redirects for SEO covering code-level redirects in [...]
November 5, 2008 @ 6:32 am
[...] htaccess Cheatsheet | marketing . web . design htaccessの設定ってすごくわすれるのでメモ。 (tags: tips apache) [...]
November 22, 2008 @ 5:05 pm
Thanks for the post. There is one that I have used many times my self. That is to rewrite the url. I use php to create sites but typically use a URL such as index.php?id=1. You can use htaccess to rewrite the url to s friendly url.
Works great.
[Reply]