Posts Tagged ‘code snippets’

.htaccess file Code Snippets

Monday, July 7th, 2008

I often have a need to add specifics to the htaccess file to make the web site meet a clients needs (or my own).  Over the years I have compiled a number of htaccess code snippets that I have used that may be useful to you if needed.

Disable Directory Browsing

Options All -Indexes

Enable Directory Browsing

Options +Indexes

If you want to block specific file types from showing you can use

IndexIgnore *.wma *.mp3 *.avi

Prefer to use Custom Error Messages

ErrorDocument 403 /forbidden.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /servererror.html

If you need SSI working with HTML instead of just SHTML
These do not all have to be used they are provided just as examples

## SSI for html files
AddType text/html .html
AddHandler server-parsed .html
## SSI for shtml files
AddType text/html .shtml
AddHandler server-parsed .shtml
## SSI for htm files
AddType text/html .htm
AddHandler server-parsed .htm

Change the Default Home Page – note the order you place them in is followed

DirectoryIndex home.html index.html index.php

Block Users from accessing the site

<limit GET POST PUT>
order deny,allow
# By the IP Address
deny from XXX.XX.XXX.XX
# By Domain
deny from
allow from all

Allow only LAN users

order deny,allow
deny from all
allow from

Redirect Visitors to New Page/Directory

Redirect oldpage.html
Redirect /olddir

Block site from specific referrers

RewriteEngine on
RewriteCond %{HTTP_REFERER} block-site\.com [NC]
RewriteCond %{HTTP_REFERER} block-site-2\.com [NC]
RewriteRule .* - [F]

Block Hot Linking/Bandwidth hogging

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]

Want to show a “Stealing is Bad” message too?

Add this below the Hot Link Blocking code:
RewriteRule \.(gif|jpg)$ [R,L]

Another example of not letting someone hotlink or use your images on their site

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$|.*php$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !domain1allowed\.com [NC]
RewriteCond %{HTTP_REFERER} !domain2allowed\.org [NC]
RewriteCond %{HTTP_REFERER} !google\. [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteRule (.*) /pathto/nohotlink.jpe

Stop .htaccess (or any other file) from being viewed

<files file-name>
order allow,deny
deny from all

To use or not to use www in your URL

# Rule for duplicate content removal : vs
RewriteCond %{HTTP_HOST} ^www.domain\.com [NC]
RewriteRule (.*)$1 [R=301,L,NC]

or in reverse

# Rule for duplicate content removal : vs
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule (.*)$1 [R=301,L,NC]

Prefer to use just and not

# Rule for not using index.php or index.html etc as home page
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^./index.php
RewriteRule ^(.)index.php$$1 [R=301,L]

Avoid the 500 Error

# Avoid 500 error by passing charset
AddDefaultCharset utf-8

Grant CGI Access in a directory

Options +ExecCGI
AddHandler cgi-script cgi pl
# To enable all scripts in a directory use the following
# SetHandler cgi-script

If you want to change script extensions

AddType application/x-httpd-php .gjw
gjw will now be treated as if they were PHP files!
The same can apply for cgi files
AddType application/x-httpd-cgi .gjw