Forum Moderators: phranque
I tried canonicalize my URL of www and index.html
To canonicalize with www and without www I implemented this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
To cannilalize with index.html and without index.html(i.e. /) I implemented this:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.example.com/$1 [R=301,L]
But when I tried both at the same time, I failed at SOME servers.
I wrote on my .htaccess like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.example.com/$1 [R=301,L]
How should I do to canonicalize w/wo www and w/wo index.html at the same time?
[edited by: jdMorgan at 3:53 am (utc) on Oct. 30, 2009]
[edit reason] example.com [/edit]
It runs for both www and non-www index requests and fixes both to / and www.
The next rule will fix all other non-www requests and fix those to www.
The .* pattern in the index rule is very inefficient. There's much better patterns you can use instead. Check prior threads discussing the index redirect for many examples.
Your www/non-www rule cannot fix a www request that has an appended port number. You need to amend your rule so that it does fix those.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html\ HTTP
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
I didn't take port numbers into consideration at this time.