Picnic Website Code Tutorials

Simple URL Canonicalization

5/31/2013

canonical links Here is the simpliest way to "rewrite non-www to www and strip index off the URL" with a very small snippet of htaccesss code. Matt Cutts discusses this premiss in some detail here. I did not write this. A very smart guy named Jeff Mott did.

# Rewrite index.html/php to folder
RewriteRule ^(.*/)?index\.(html?|php)$ /$1 [R=301,L]

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

Need to exclude a folder from either of the above rules? Then simply add the following line directly before each to exclude either.

# Excludes forum folder
RewriteCond $1 !^forum(?:$|/)
		

Sponsors

Top Donators

Friends of Mine