htaccess tricks for all

Apache module ‘mod_rewrite’ must be installed & ‘Rewrite Engine’ must be set to ‘On’ for these tricks to work;

# enable rewriting
RewriteEngine on

Missing trailing slashes fix:

Options +FollowSymlinks
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)\..+/(www\.)|.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(#)?(/)?(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)\..+/(www\.))$ [NC]
RewriteRule ^ http://%6%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]

Change default index.html page:

DirectoryIndex mypage.html

Custom error pages:

ErrorDocument 400 /MySite/errors/400.php
ErrorDocument 401 /MySite/errors/401.php
ErrorDocument 403 /MySite/errors/403.php
ErrorDocument 404 /MySite/errors/404.php
ErrorDocument 500 /MySite/errors/500.php

Prevent viewing of a specific file:

<files settings.php>
 order allow,deny
 deny from all
</files>

Prevent viewing of a specific extention:

<files *.extention>
order allow,deny
deny from all
</files>

Prevent execution of specific scripts:

Options -ExecCGI
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi

Show download file prompt:

AddType application/octet-stream .mp3 .mpeg .mov .pdf .doc

Disable directory browsing:

Options All -Indexes
IndexIgnore *

Disable display of specific files in directory:

IndexIgnore *.jpg *.mp3 *.mpeg

Remove www:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

~ by culpritz on April 19, 2009.

Leave a Reply