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 .*$ http://www.domainname.com [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 domainname.com to www.domainname.com
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 http://www.domainname.com/index.php to http://www.domainname.com
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domainname.com [R=301,L]

ErrorDocument 404 http://www.domainname.com/sitemap

RedirectMatch "/old-page" /new-page

#sub.domainname.com will be redirected to domainname.com/sub
RewriteCond %{HTTP_HOST} ^sub\.domainname\.com
RewriteRule ^.*$ http://www.domainname.com/sub  [R=301,L]

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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