Forum Moderators: phranque

Message Too Old, No Replies

two different versions

fix non-www to www

         

Lorel

10:21 pm on Mar 6, 2010 (gmt 0)

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




I just noticed I have two versions of the non-www to www fix:

on first site:

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

and 2nd site:

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

The 2nd lines are different.

Can someone tell me if one of these is wrong?

g1smd

10:34 pm on Mar 6, 2010 (gmt 0)

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



They will only be 'wrong' if they don't do what you want them to do.

So, what do you want them to do?


One of them redirects ALL non-www requests for all pages, all images, all CSS files, all JS files, robots.txt, and so on, to the root '/' of the www version of the site.

The other preserves the requested path and file information from the original request when redirecting to the new www version of the URL.

Both of them fail to redirect www requests with an appended port number and/or trailing period after the domain name. That is likely to be an error. They should also be redirected.

Lorel

12:29 am on Mar 7, 2010 (gmt 0)

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



I just want normal behavior.

I notice that when I am already on the first site above, if I copy/paste a url in the browser (to see a new page I just loaded) it reverts to the home page, which I don't want.

Is the 2nd one correct?

Both of them fail to redirect www requests with an appended port number and/or trailing period after the domain name. That is likely to be an error. They should also be redirected.


I've never seen this discussed before. Can you explain or point me in the right direction please?

jdMorgan

5:41 am on Mar 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is, unfortunately, no fixed definition of "normal behavior" -- What's normal for someone else may not be at all what you want.

Here's a third version, "normal" if you want all non-canonical hostnames redirected to the same URL-path on the canonical host:

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

That uses an exact match, so if the requested hostname is not exactly "www.example.com", then the request is redirected to www.example.com.

This will redirect all of the following (and many more) requests for non-canonical hosts:
http://example.com (non-www, and all variants of non-www) 
http://www.example.com. (FQDN)
http://www.example.com:80 (port number appended)
http://www.example.com.:80 (both)
http://WwW.eXaMpLe.com (casing)
http://foo.example.com (non-www subdomain)

Further, any https request to a non-canonical hostname will be redirected to http on the canonical host. This is of no consequence if your site doesn't use https, but would be unwanted behavior if it did.

So, none of this is simple. I suggest that you not use any code copied from anywhere unless you fully understand its function and its SEO effects. Keep in mind that .htaccess code is server configuration code and the tiniest of errors can have distatrous effects on the operation of your server and/or your search engine rankings. This is not a copy-and-paste-friendly activity. It is possible, with a single typo, to put yourself out of business.

The resources cited in our Apache Forum Charter and the threads in our Library section may be useful in that regard.

Jim

jdMorgan

6:40 pm on Mar 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh and here's a fourth and a fifth, because of an issue that came up in another thread:

The third solution posted above can be safely used on name-based virtual hosting. But on IP-based hosting, you could get an HTTP/1.0 request with an HTTP Host header, leaving the %{HTTP_HOST} variable blank, and that would cause an 'infinite' redirection loop. This solution is more general:

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


And to address the http/https issue I mentioned above:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.*)$ http%2://www.example.com./$1 [R=301,L]

Jim