.htaccess cheat sheet

A quick cheat sheet for working with .htaccess files and Apache under Linux. Many of these options/features are also available via the control panel.

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]

Reproduced in part from Zach Graeves blog, there’s also a good post on 301 redirects for SEO covering code-level redirects in different web languages.

2 Responses to “.htaccess cheat sheet”

  1. zacheos Says:

    An updated and slightly corrected version of this post is available directly at my site. I recommend that this post be updated to reflect the changes made to the original article. Thanks!

  2. Carl Says:

    Thanks for the reply and the original article! The post has been updated to reflect the changes.

Leave a Reply