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 .domain.com
allow from all
</limit>
Allow only LAN users
order deny,allow
deny from all
allow from 192.168.0.0/24
Redirect Visitors to New Page/Directory
Redirect oldpage.html http://www.domain.com/newpage.html
Redirect /olddir http://www.domain.com/newdir/
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\.)?mydomain.com/.*$ [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)$ http://www.mydomain.com/nosteal.gif [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
</files>
To use or not to use www in your URL
# Rule for duplicate content removal : domain.com vs www.domain.com
RewriteCond %{HTTP_HOST} ^www.domain\.com [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L,NC]
or in reverse
# Rule for duplicate content removal : domain.com vs www.domain.com
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L,NC]
Prefer to use just domain.com/ and not domain.com/index.php?
# Rule for not using index.php or index.html etc as home page
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^./index.php
RewriteRule ^(.)index.php$ http://domain.com/$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