htaccess Cheatsheet

, ,
  • Digg
  • Technorati
  • del.icio.us
  • Propeller
  • StumbleUpon
  • Reddit
  • Mixx
  • Fark
  • Slashdot
  • Sphinn
  • Design Float
  • DZone
  • BlinkList
  • email
October 29th, 2008 | Web Development How-to's | 10 Comments

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

Welcome Back!

Now is your chance to join our growing community! Be the first to know about great new articles like this one. Register for my Email Updates or RSS Feed today!

About the Author

Zach is a marketing consultant and owner of a FL web design and marketing group. He offers graphic design and marketing services, speaks on Internet marketing, and blogs about it all in his free time. (»)

Contact Zach: Company Website | Email

Related Posts


Filter Yourself From Google Analytics

301 Redirect For SEO

Top Ping URLs & Update Services

Allow HTML in WordPress Author Bios

HTML Email Signature in Gmail Using Google Chrome


10 Comments

  1. Bill

    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.

  2. Pipe Ten Web Hosting Resource » Blog Archive » .htaccess cheat sheet

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

  3. ズゴックNo.1 - links for 2008-11-05

    November 5, 2008 @ 6:32 am

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

  4. Jamie Larsen

    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

  5. low-cost reliable web hosting

    March 16, 2009 @ 9:32 am

    Good post – thanks for sharing!

    Reply

  6. Tagz | "htaccess Cheatsheet | marketing . web . design" | Comments

    May 16, 2009 @ 9:58 am

    [...] [upmod] [downmod] htaccess Cheatsheet | marketing . web . design (www.zachgraeve.com) 2 points posted 6 months, 2 weeks ago by SixSixSix tags 4mdelicious [...]

  7. .htaccessに関する覚え書き - IDEA*IDEA ~ 百式管理人のライフハックブログ

    August 3, 2009 @ 10:35 pm

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

RSS feed Comments | TrackBack URI

Write Comment