Picnic Website Code Tutorials

Canonicalization htaccess mod_rewrite 301 redirect non-www to www and index.html(htm,php) to folder Tutorial

Or here is a much more simple version doing the same thing.

Matt Cutts of Google discusses Canonical URLs and the canonical link element. However, he also goes on to say that it's better taken care of upstream as the code shown below takes care of.

If you've been searching on the web for this .htaccess 301 redirect code then no doubt you've seen the huge variations of many - many examples. Which one is right? THIS ONE IS. I don't know much about htacces code, but I did find someone that did when I needed this bit of code. In short, left as server default, the following URL's all go to the same place, and Google (Bing etc) see these as seperate domains, and therefore duplicate content. Thats not what you want.

mydomain.com
mydomain.com/
mydomain.com/index.html
mydomain.com/index.htm
www.mydomain.com
www.mydomain.com/
www.mydomain.com/index.htm
www.mydomain.com/index.html

That's a short list. Apparently there is a lot more as well. Below you will find "THE" best code to take care of this issue and 301 permanent redirect all those above listed domains to one single domain that will look like this http://www.mydomain.com/. Therefore all your page rank juice will flow into one domain and not be split up among 10. Many people have written much on this issue so I will not. I will however list all the links that I used to light my way. A very smart member named g1smd in a WebMasterWorld Forum Post gave me all the code I needed and answered all my questions about it. However, I would advise you not to take my word for it (changing your servers code is no small deal), and thoroughly read through that forum post. If you have additional questions, ask them there, as I'm sure he or someone else would be more than happy to help. Perishable Press has an excellent right up on htaccess how to's and code. And after you have installed the code use Redirect Check SEO Tool to test your redirects.

A little explanation on some common variants you see on the web: (1) RewriteEngine On only needs to be listed once at the beginning. (2) Options +FollowSymLinks in some server configurations, mod_rewrite requires FollowSymLinks to be enabled, or it will crater with a 500-Server Error. If your mod_rewrite code works without the Options +FollowSymLinks directive, that means that your server configuration file has enabled them already, and you won't need that directive in your .htaccess files. (3) The order is important. So only use in the order shown. (4) All of these except one specifically mentioned below redirect all index files in all subfolders (aka subdirectories) of the website.

How to edit, create, and upload your .htaccess file: Easy! With windows Notepad (or similar) create a htaccess.txt file. Insert the code. Before you FTP it simply change it to .htaccess(no .txt). (dot)htaccess(no .txt). The htaccess goes in your root folder. After you transferred it change it back to .txt extension (on your machine only - not the server) so you have access to it again.

With HTML Extensions

# 301 permanent redirect index.html(htm) to folder

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.mydomain.com/$1 [R=301,L]
		

# 301 permanent redirect non-www (non-canonical) to www

RewriteCond %{HTTP_HOST} !^(www\.mydomain\.com)?$
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
		

And Put Them Together

# mod_rewrite On only needed once
RewriteEngine On

# 301 permanent redirect index.html(htm) to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.mydomain.com/$1 [R=301,L] 

# 301 permanent redirect non-www (non-canonical) to www
RewriteCond %{HTTP_HOST} !^(www\.mydomain\.com)?$
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
		

With PHP Extensions

# mod_rewrite On only needed once
RewriteEngine On

# 301 permanent Redirect index.php to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.mydomain.com/$1 [R=301,L]

# 301 permanent Redirect non-www (non-canonical) to www
RewriteCond %{HTTP_HOST} !^(www\.mydomain\.com)?$
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L] 
		

Universal Code: With HTML and PHP Extensions

# mod_rewrite On only needed once
RewriteEngine On

# 301 permanent Redirect index.html(.htm and .php) to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ http://www.mydomain.com/$1 [R=301,L]

# 301 permanent Redirect non-www (non-canonical) to www
RewriteCond %{HTTP_HOST} !^(www\.mydomain\.com)?$
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L] 
		

However! If you have addon domains... so when you add a website it creates another folder in your root... then this code above will direct your addon domains to http://www.maindomain.com/addondomain.com/. That's bad! So the simple fix is to exclude the addon domains in both rule sets as shown below.

mod_rewrite Exclusion For Addon Domains

RewriteCond %{HTTP_HOST} !(addondomain1\.com|addondomain2\.com|addondomain3\.com) 
		

And put it all together with (PHP and HTML Extensions)

# mod_rewrite On only needed once
RewriteEngine On

# 301 permanent redirect index.html(.htm and .php) to folder with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(addondomain1\.com|addondomain2\.com|addondomain3\.com) 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ http://www.mydomain.com/$1 [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(addondomain1\.com|addondomain2\.com|addondomain3\.com) 
RewriteCond %{HTTP_HOST} !^(www\.mydomain\.com)?$
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L] 
		

# 301 permanent redirect index.html(htm) to ONLY the root (not all folders)

# 301 permanent redirect index.html(htm) to ONLY the root (not all folders)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L] 
		

Your Welcome

Sponsors

Top Donators

Friends of Mine