Saturday 04 February 2012

htaccess Cheatsheet

11002000962

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

About the author

Founder of Eos Designs and co-founder of iPrintEasy and a number of other web businesses - an entrepreneur with over 10 years of experience in Internet Marketing - Zach understands what it takes to be successful online and specializes in the development of advertising campaigns and marketing plans for start-ups.
  • Bill

    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.

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

      • 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]

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

  • Pingback: Pipe Ten Web Hosting Resource » Blog Archive » .htaccess cheat sheet

  • Pingback: ズゴックNo.1 - links for 2008-11-05

  • http://www.crazylarsens.com Jamie Larsen

    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.

  • http://www.dcgwebdesign.com low-cost reliable web hosting

    Good post – thanks for sharing!

  • Pingback: Tagz | "htaccess Cheatsheet | marketing . web . design" | Comments

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

  • http://411newyork.org 411 New York

    If you had an old site called zach-graeve.com and wanted to use it as a domain alias of zachgraeve.com so you would not have to have hosting for two domains, what would you do?

    Even though there is a main htaccess file, the wordpress press folder (with the htaccess code from your image above) stops zach-graeve.com from 301 redirect to zachgraeve.com and may eventually cause duplicate content.

  • http://www.geoklix.com LOCAL SEO SERVICES

    Hello dear.You have written a great post. Going to share with my followers on twitter. Thanks for sharing.

  • http://www.jmawebconsulting.com Web Hosting Company

         Your post is rocking
    and knowledgeable… I really appreciate the way you write . I would like to
    read more from you         

  • http://housesearch123.com Homes in orange county

    Hello, A very
    insightful post. Thanks for the info. Its great that if our default settings
    are giving us messy or stringy builds, this dialog can probably help.Thanks
    for the information. 

  • http://www.sleekandchicboutique.com/ Boutique jewelry

    wow great post, there are tones of comments here which is really awesome. Great work and keep going to post more useful content.

  • http://www.carpetcleanersacramento.com/ carpet cleaner Sacramento

    I wish to say that this post is amazing, nice written and include approximately all vital infos. I’d like to see more posts like this.

Powered by WordPress | Privacy Policy | Web Design Miami