Forum Moderators: phranque

Message Too Old, No Replies

To canonicalize w/wo www and w/wo index.html at the same time

I tried them at the same time but failed. Why?

         

suzukik

11:50 pm on Oct 29, 2009 (gmt 0)

10+ Year Member



I know there are a lot of threads here regarding my question but I just want to make it clear.

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]

It works well.

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]

It works well, too.

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]

I found something wrong after collecting information but don't know what it is.

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]

g1smd

12:30 am on Oct 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Put the index rule first.

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.

suzukik

1:05 am on Oct 30, 2009 (gmt 0)

10+ Year Member



How about this?

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.

jdMorgan

3:57 am on Oct 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Jim

suzukik

4:03 am on Oct 30, 2009 (gmt 0)

10+ Year Member



Thank you, Jim.

I have to learn regular expressions...

jdMorgan

1:48 pm on Oct 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you do... And you'll find that doing so benefits your coding skills in practically all scripting languages, as well as high-level programming languages such as 'C' and others. It's well worth the time invested.

Jim