htaccess Cheatsheet
apache, htaccess, redirects
As 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







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.
Comment by Bill — November 2, 2008 @ 9:05 am
[...] in part from Zach Graeves blog, there’s also a good post on 301 redirects for SEO covering code-level redirects in [...]
Pingback by Pipe Ten Web Hosting Resource » Blog Archive » .htaccess cheat sheet — November 2, 2008 @ 9:35 am
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.
Comment by zacheos — November 2, 2008 @ 10:44 am
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]
Comment by Bill — November 2, 2008 @ 6:45 pm
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.
Comment by zacheos — November 2, 2008 @ 6:48 pm
[...] htaccess Cheatsheet | marketing . web . design htaccessの設定ってすごくわすれるのでメモ。 (tags: tips apache) [...]
Pingback by ズゴックNo.1 - links for 2008-11-05 — November 5, 2008 @ 6:32 am
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.
Comment by Jamie Larsen — November 22, 2008 @ 5:05 pm
Good post – thanks for sharing!
Comment by low-cost reliable web hosting — March 16, 2009 @ 9:32 am
[...] [upmod] [downmod] htaccess Cheatsheet | marketing . web . design (www.zachgraeve.com) 2 points posted 6 months, 2 weeks ago by SixSixSix tags 4mdelicious [...]
Pingback by Tagz | "htaccess Cheatsheet | marketing . web . design" | Comments — May 16, 2009 @ 9:58 am
[...] » htaccess Cheatsheet | marketing . web . design [...]
Pingback by .htaccessに関する覚え書き - IDEA*IDEA ~ 百式管理人のライフハックブログ — August 3, 2009 @ 10:35 pm