htaccess
htaccess Files Best Practice Guide With Examples and Functions
RewriteEngine On #----------------------------------------- # Redirect Entire two Site to New Domain #----------------------------------------- # 1. RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR] RewriteCond %{HTTP_HOST} ^another.olddomain.com$ [NC] RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L] # 2. LoadModule rewrite_module modules/mod_rewrite.so <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^localhost$ [NC] RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L] </IfModule> # Redirect Entire Site to New Domain #----------------------------------------- RewriteCond %{HTTP_HOST} ^old.domain.com$ RewriteRule ^/?$ "http\:\/\/new\.domain\.com\/" [R=301,L # Redirect Entire Site to New Site #----------------------------------------- Redirect 301 / http://www.new-domain.com Redirect 301 /old-page-name http://www.your-domain.com/new-page-name # 301 Redirect Based on IP Address: #----------------------------------------- RewriteCond %{REMOTE_ADDR} ^(A\.B\.C\.D)$ RewriteRule ^/* http://www.yourdomain.com/access-denied.html [L] # Non-www to www Redirect # This is a very common function for redirecting your site # at the top level. Sometimes a site can be accessed at # http://domain.com/ and http://www.domain.com/ which can # cause duplication and site management issues. #----------------------------------------- RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] # Clean up URLs to remove file extensions # If you wanted to make your URLs look a little nicer by # removing the file extension from the end of it you can # use the function below. In order for this to work # internally to the site you should also ensure that all # the linking within the site has the extension manually # removed. #----------------------------------------- RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html # Restrict access # To add an element of security to the .htaccess file you # should add the following function. It will deny access # to anyone attempting to get to the file externally to # view or change it. For example, if you were to add this # to your file and then attempt to access it at # http://www.domain.com/.htaccess you will be presented # with a 403 forbidden error. This means its working! #----------------------------------------- <Files .htaccess> order allow,deny deny from all </Files>