usefull heaccess

Posted: April 14, 2011 in PHP

Options +FollowSymLinks
Options +Indexes
RewriteEngine On

#if any extra queryparameteres is passed e.g. home?q=extra it is redirected to 404 error
RewriteCond %{REQUEST_URI} !/ajax.php
RewriteCond %{QUERY_STRING} !^$
RewriteRule .*$ [R=404,L]

#Remove trailing slash
RewriteCond %{HTTP_HOST} ^(www.)?domainname\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] 

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{REQUEST_URI} !/ajax.php
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule ^([^\.]+)\.php$ /$1 [L,R=301]

#/home work as /home.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#redirect to
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=301,L]

#redirect to
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ [R=301,L]

ErrorDocument 404

RedirectMatch "/old-page" /new-page will be redirected to
RewriteCond %{HTTP_HOST} ^sub\.domainname\.com
RewriteRule ^.*$  [R=301,L]

# restrict ip address
order allow,deny
deny from
allow from all

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s