Forum Moderators: Robert Charlton & goodroi

Message Too Old, No Replies

Canonical issue: non-www redir to www. Still needed?

         

centime

12:38 pm on May 3, 2008 (gmt 0)

10+ Year Member



Hi All

Do people still find it beneficial to redirect from non-www to www. via 301 permanent redirect ?

Cheers

youfoundjake

2:03 am on May 4, 2008 (gmt 0)

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



I still employ it on all my sites, as well as redirecting index.* to / as well.
Why confuse the engines when it only takes 5 minutes to set up in .htaccess?

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

tedster

6:19 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While establishing your preferred form of the domain in Webmaster Tools will stop the other form from being shown in the SERPs, there's no guarantee on how well Google "combines" the backlink juice. Some people who use only the Webmaster Tools preference report still seeing different toolbar PR for different canonical forms of the same page.

And then there's the simple fact that there are other seach engines to think about. For me, having the technical set up on the server is still an important step - and it will still be so for a long time, I'd project.

annej

6:29 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My main page dropped from page one to waaaay down there for a few months. After trying everything I could think of to undo what I thought was a penalty I realized that I'd inadvertently taken down my .htaccess when I did a major redesign on my site. Once I put it back my page gradually made it back to the first page.

What I don't know is if it was just a matter of Google seeing it as two pages (www and none) or if the page was hijacked. Can that still happen?

Robert Charlton

6:40 pm on May 4, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



annej - You need to look at whether you had backlinks to both the www and non-www variants of your site. If so, then taking down .htaccess would have caused your inbound PageRank to get split among the two sets of pages.

I think it's not likely that WMT is as good as proper 301 redirects in fixing this.

jdMorgan

8:20 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Minor corrections/speed-up to code posted above to prevent proliferation.

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

Jim

annej

6:37 am on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Robert, My site is really old so people could have linked with both www and not over the years. Fortunately the problem seems to be solved now that I got the .htaccess back in.

zeus

9:53 am on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just had the problem again, for a short time my htaccess was not correct, then I saw non www in the index so yes you need this redirect

Marcia

9:58 am on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not having that redirect can KILL you at MSN/Live Search, and you'd best check out index.htm and index.html as well, especially if it seems your site has mysteriously disappeared.

trader

12:38 pm on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

I am wondering about the difference between code I am using above and the code posted by Jim (in addition to the obvious difference in that I prefer to not use www)? The last line has a disparity involving: ^(.*)$ and the order in [L,R=301] which order I imagine does not matter, with ^(.*)$ looking more significant?

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

TheMadScientist

1:26 pm on May 8, 2008 (gmt 0)

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



You are correct, order should not have any impact (AFAIK - I actually use the other order), but the regular expression, capitalization, and flag edits can.

When using a 'catch-all' there is no need for the start and end anchor tag, so they are basically 'extra characters' in the file, which are absolutely unnecessary. .* will match 0 or more of anything, except the end of a line, so it matches every possible URL start to finish. The () store the match for back-reference. Since you will be checking this ruleset on every request it is a good idea to eliminate anything not absolutely necessary.

If you were matching a specific pattern anchoring would be important, not only to match correctly, but also to improve efficiency by eliminating some request from the comparison as soon as possible.

Another edit to the first ruleset was the addition of the \ preceding the ., which is necessary to match a literal dot rather than, 'any character except the end of a line', as a . matches. EG example.com will actually match exampleTcom, example1com, etc. It should not really matter here, except for correctness and form (which are always good to practice and have correct) since exampleTcom.com will not resolve to your domain.

It appears the capitalization was also corrected, the NC (no case) was removed (unnecessary due to the catch-all), and the L (Last) flag was added. The capitalization matters, although it is often translated correctly, but on some machines it can cause issues. The NC was an unnecessary flag, and the L flag is required unless you KNOW you do not need it. Failure to use a L flag on a ruleset can cause some very unexpected results, so unless you are doing quite a bit of rewriting and know when you should use or remove it, it is recommended to use it.

Personally, I like using a negative match, especially if you have wild-card subdomains on.

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

The above 'says' if the host ! (is not) www.example.com EXACTLY, and ! (is not) empty (the ? following the () pattern makes it optional - necessary for HTTP 1.0 clients), redirect any domain requested to www.example.com/The-URL-Requested

EDITED: I Was being silly early in the morning, so I had to remove a nonsensical line from my post. I'm done editing now. Really this time, sorry mods.

[edited by: TheMadScientist at 1:40 pm (utc) on May 8, 2008]

hibernate

5:10 pm on May 31, 2008 (gmt 0)

10+ Year Member



I currently rank #1 for one of my keyphrases for "example.com" (no www)

On Google Webmaster Tools my site is listed as "example.com" and my XML sitemap has the all the pages listed as "http://example.com" ; again without www

It's a growing niche and I have recently gotten more aggressive about developing backlinks and many of my new backlinks link to www.mysite.com and I'm worried about diluting my PR.

I'm a bit confused as to how to write my .htaccess; which of the following is what I want and if someone could be so kind as to explain why so that I might learn.
-----------------------------
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

or

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

[edited by: tedster at 6:00 pm (utc) on May 31, 2008]
[edit reason] switch to example.com - it can never be owned [/edit]

tedster

6:05 pm on May 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume you want to preserve the no-www urls and avoid seeing the with-www urls in the index, right

Think of RewriteCond as saying "if" and RewriteRule as "then". The first choice you gave says "if there is no www in the requested url, then add it." That goes in the opposite direction of what you want. So you would choose option #2.

jdMorgan

6:30 pm on May 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Neither code snippet #1 or #2 will correct a period and/or a port number appended to the hostname -- and either or both would result in perfectly-valid, but non-canonical hostnames.

Jim

g1smd

7:16 pm on May 31, 2008 (gmt 0)

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



*** Still needed? ***

No.

Always needed.

g1smd

7:17 pm on May 31, 2008 (gmt 0)

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



*** Does the order matter? ***

I always put the [L] last as it signifies "last" anyway.

hibernate

9:34 pm on Jun 1, 2008 (gmt 0)

10+ Year Member



I picked option two.

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

It was the only text in my .htaccess file and I got the following when trying to access my page:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, support@supportwebsite.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
--------------------------------------------------------------------------------

Apache/1.3.33 Server at ["example.com"] Port 80

PS Mods: this now may be more of a coding issue than a google search issue so if I need to move this to another forum let me know.

tedster

9:53 pm on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes - it does sound like an Apache server [webmasterworld.com] issue for you that may not have general value in this forum. So it's probably best to start a new thread over there.

centime

12:01 pm on Jun 17, 2008 (gmt 0)

10+ Year Member



Hi Guys

I am new to linux/apache and I've used the code on this trhead. It works fine on one script I am using ,

but on the other script, which is installed in a sub directory, it does not work on the pages in the sub directory,

There are 2 .htaccess, one at the root and one in the sub directory, initially I put the code just in the root directory .htaccess, it works fine for pages in the root directory , but the pages of the script in the sub folder just ignored it,

so I then placed the redirect script on the .htaccess of the sub folder, and still no joy

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

also tried

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

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

Would appreciate some help

Cheers

jdMorgan

3:19 am on Jun 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your subdirectory rule will not work because you've included "/subfolder" in a RewriteCond pattern which you require to match against HTTP_HOST. Since HTTP_HOST contains *only* the hostname, and no part of the local URL-path, your pattern will never match. Remove "/subfolder" from the HTTP_HOST pattern and test again.

Further, it should be unnecessary to put the rule in both the root and the subfolder; Any code in root should be executed before Apache traverses to the subfolder. If this isn't working, add


RewriteOptions inherit

to the mod_rewrite code in the root folder.

See the Apache mod_rewrite documentation for information on the values of server variables available to RewriteCond and RewriteRule.

Jim