Forum Moderators: phranque

Message Too Old, No Replies

Canonical 301 isn't working with Rewrite

www is missing

         

Lorel

4:08 pm on Aug 26, 2009 (gmt 0)

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



I have a problem with the canonical redirect while also redirecting the old home page that was in a folder, to the root. The canonical worked until I put in the redirect of home page to root but the www is not included now.

Can anyone tell me what's wrong here:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^5201\.html$ http://www.example.com/ [R=301,L]

jdMorgan

4:56 pm on Aug 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rules are not in the correct order: Put the most specific rule (the single old home page redirect) first to avoid a chained redirect if that old home page is requested from the non-canonical domain.

Please give us an example of what URL "doesn't work," what URL you requested, what you expected, what actual result you got, and how the expected and actual results differ. While it may seem obvious to you, it's especially important to be very, very clear when "odd" results such as this are encountered.

Where is this code located, in .htaccess or in a server config file?
Do you have any other rules in this file or in others that may be interacting with these rules to produce unexpected results?
Do you have any scripts that generate redirects?

Jim

Lorel

5:53 pm on Aug 26, 2009 (gmt 0)

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



Hi Jim,

I'm working with an htaccess file. All other features in the htaccess file are working like:

ErrorDocument 404 /missing.html
AddHandler server-parsed .html

I moved the rewrite rule for home page to just after RewriteEngine on and that works fine.

However, I have other pages I'm redirecting also and when I moved that command below the one mentioned above and removed the www. from browser and entered the old url, and clicked in browser window the rewrite rule just brings up the old page. IF I click on it a 2nd time then it redirects properly to the new page with the www showing.

This one is in a folder so maybe I didn't write it correctly:

RewriteRule ^5441/index\.html$ http://www.example.com/example.html [R=301,L]

The error code, parser code and 301 for www for home pg works fine.

g1smd

6:41 pm on Aug 26, 2009 (gmt 0)

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



Clear your browser cache and try again.

Rule order is important. You want any URL request to be matched by only one rule and for that rule to completely solve all the things that are wrong with that request.

Lorel

6:48 pm on Aug 26, 2009 (gmt 0)

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



Hi g1smd,

Are you saying the redirects to home page (cannonical and new home page location) should be written as one command? If so I'm not sure how to write it.

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^5201\.html$ http://www.example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Lorel

6:56 pm on Aug 26, 2009 (gmt 0)

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



I tried another browser after listing items as they are above and when I entered the url without the www and clicked, it resolved to the new page with www included.

So the later problem was due to not clearing the cache in the browser.

thanks guys

g1smd

8:18 pm on Aug 26, 2009 (gmt 0)

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



*** Are you saying the redirects to home page (canonical and new home page location) should be written as one command? ***

No. I am saying that you should have one rule that fixes a "page" but that for the "page" it should force "http" and "www" at the same time.

After that you might have another rule that fixes some other "page" and that for that "page" it should force "http" and "www" at the same time.

Finally, you will have a rule that fixes all non-www requests and forces "http" and "www". This rule never runs for the above-mentioned pages because previous rules have already fixed them.

So, for any particular request, only one rule runs. If the rules were in some different order, multiple rules would run, one to change to www and another to strip the filename. You must avoid this redirection chain.

Lorel

8:51 pm on Aug 26, 2009 (gmt 0)

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



Thanks for explaining. I think I "finally" understand :)