12 Most Used .htaccess Examples, Tips – Commands & Directives

by Yang Yang on March 27, 2007

Updated: added a 301 redirect directive.

Whatever you wanto call this, a cheat sheet, a real thin manual, or anything, we simplify the use of .htaccess as much as possible and make it do more for you with less hassle.

  1. Unify kavoir.com and www.kavoir.com!
    # Use a 301 redirect from kavoir.com to www.kavoir.com to tell SEs that the 2 domains are the same thing so there's no PageRank leakage.
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^kavoir\.com [NC]
    RewriteRule ^(.*)$ https://www.kavoir.com/$1 [R=301]
  2. Block spammers!
    # This blocks 218.1.132.177 and *.spammers.com from accessing your site.
    <limit GET POST PUT>
    order deny,allow
    deny from 218.1.132.177
    deny from .spammers.com
    allow from all
    </limit>
  3. Change default page!
    # The order is followed as specified:
    DirectoryIndex default.htm default.php index.html index.php
  4. Enable directory browsing!
    Options +Indexes
    # block a few types of files from showing:
    IndexIgnore *.wmv *.mp4 *.avi
  5. Disable directory browsing!
    Options All -Indexes
  6. Customize error messages!
    ErrorDocument 403 /forbidden.html
    ErrorDocument 404 /notfound.html
    ErrorDocument 500 /servererror.html
  7. Get SSI working with HTML/SHTML!
    AddType text/html .html
    AddType text/html .shtml
    AddHandler server-parsed .html
    AddHandler server-parsed .shtml
  8. Redirect it!
    Redirect oldpage.html http://www.domainname.com/newpage.html
  9. Block visits or leeches from specific referers!
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} site-to-block\.com [NC]
    RewriteCond %{HTTP_REFERER} site-to-block-2\.com [NC]
    RewriteRule .* - [F]
  10. Stop a file from being viewed!
    # mycontacts.txt cannot be accessed by any means:
    <files mycontacts.txt>
    order allow,deny
    deny from all
    </files>
  11. Password-protect a directory!
    See http://thejackol.com/scripts/htpasswdgen.php
  12. Change script extensions!
    # anyfile.kv will be treated like a PHP script:
    AddType application/x-httpd-php .kv
    # anyfile.kvcgi will be treated like a CGI script:
    AddType application/x-httpd-cgi .kvcgi
Alan July 11, 2008 at 7:22 am

Great sum up. I found 9 and 10 the most useful for me.

Yang Yang December 8, 2008 at 11:29 pm

I usually use 9 to prevent image leeches.

Yogesh Malik September 30, 2009 at 7:29 pm

very useful and practical example, i was looking for something like this. Great information all over on this website.

Yang Yang February 26, 2010 at 2:28 pm

Thanks. You’ve got a great website too!

Andrew February 17, 2011 at 9:14 pm

Yes, sure, .htaccess is most power tool for website

Comments on this entry are closed.

Previous post:

Next post: