Home Daily Marketing Tips About Me Work With Me

Marketing & Design Blog

Internet Marketing, SEO, Blogging, Social Media, Lead Generation and Analytics

htaccess Cheatsheet

, ,

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.html

OR

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

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

10 Comments »

  1. 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

  2. [...] 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

  3. 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

  4. 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

  5. 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

  6. [...] htaccess Cheatsheet | marketing . web . design htaccessの設定ってすごくわすれるのでメモ。 (tags: tips apache) [...]

    Pingback by ズゴックNo.1 - links for 2008-11-05 — November 5, 2008 @ 6:32 am

  7. 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

  8. Good post – thanks for sharing!

    Comment by low-cost reliable web hosting — March 16, 2009 @ 9:32 am

  9. [...] [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

  10. [...] » htaccess Cheatsheet | marketing . web . design [...]

    Pingback by .htaccessに関する覚え書き - IDEA*IDEA ~ 百式管理人のライフハックブログ — August 3, 2009 @ 10:35 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

logo design brand development

We have simplified the typical logo ordering process, put you in direct contact with the artist, made getting additional branding products and websites easy, and rounded it all off with high quality printing services at fantastic rates... Learn more

website design development

We have made getting a completely dynamic website built on the WordPress content management system with a theme of your choice easy. We integrate your logo into the site design and offer great add-ons at amazing prices - giving you a range of choices on design and layout... Learn more

search engine optimization

We have expert SEO consultants ready to tackle any aspect of your search engine marketing and optimization needs. We take pride in the ability to custom build an Internet marketing campaign that is perfectly suited to your needs.... Read more


© Copyright 2003-2010 Zach Graeve and Expert Online Services, LLC. All Rights Reserved.
 Subscribe, RSS, Comments RSS, XML Sitemap, Privacy Policy