Forum Moderators: phranque

Message Too Old, No Replies

Canonicalization for all domains on server

non-www to www, canonicalization, multiple domains, all domains

         

dadonk

3:11 pm on Jan 18, 2011 (gmt 0)

10+ Year Member



I've seen examples like the following for 301 redirecting non-www to www domains:


RewriteCond %{http_host} !^www.example.com$ [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc,L]


I have a bunch of different domains on one server that I would like to do this for. How can I make the domain part of this dynamic so that I only have to put 1 set of rules in and not have to specify a set of rules for each domain?

g1smd

3:31 pm on Jan 18, 2011 (gmt 0)

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



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


That code doesn't just redirect non-www to www.

What it should do is redirect anything that is not EXACTLY www.example.com to www.example.com.

Note that the [NC] has been removed as including it interferes with the correct intended operation. Literal periods in patterns should also be escaped. Also fixed the capitalisation of server variables and flags.

It therefore redirects any and all of these:

example.com
example.com.
example.com:80
www.example.com.
www.example.com:80
WWW.Example.Com
otherdomain.net
otherdomain.net:80
www.otherdomain.org:80

and so on.

It caters for all domains, all subdomains, trailing dot, port numbers, and upper case requests.

dadonk

3:42 pm on Jan 18, 2011 (gmt 0)

10+ Year Member



sorry, if I wasn't clear. The followin is my goal.

I want to have multiple domains have all non-www go to www but I don't want to put in separate rules for each domain.

example.com => www.example.com
example2.com => www.example2.com
example3.net => www.example3.net
example4.org => www.example4.org

dadonk

3:49 pm on Jan 18, 2011 (gmt 0)

10+ Year Member



to elaborate, I want to know how to do this:

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

But have the domain be a wildcard

RewriteCond %{HTTP_HOST} !^(www\.WILDCARD\.WILDCARD)?$
RewriteRule ^(.*)$ [REPLACEDFROMABOVE.REPLACEDFROMABOVE...] [R=301,L]

g1smd

4:02 pm on Jan 18, 2011 (gmt 0)

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



You can't capture the wildcard items into a backreference when the pattern is for a negative match, so this type of redirect can't be used "as is".

A slightly different approach is needed, likely needing two rules:
- one for any type of non-www request, both with or without periods and/or port numbers, etc.
- one for any www request that also contains trailing period and/or port number, etc.

dadonk

6:47 pm on Jan 18, 2011 (gmt 0)

10+ Year Member



I was wondering if anyone could help me out with a working example?

g1smd

7:33 pm on Jan 18, 2011 (gmt 0)

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



You need two rulesets. Each will have a Condition and a Rule.

I have listed what both of the new RewriteConds need to do, use the existing condition as a template.

The two RewriteRules will be the same as they are now.

jdMorgan

11:57 pm on Jan 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Externally redirect non-canonical non-blank hostname requests to canonical "www" hostnames
RewriteCond %{HTTP_HOST} !^www\.[a-z0-9][a-z0-9\-]+[a-z]\.([a-z]{3,6}|(co\.)?[a-z]{2})$ [NC]
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*([a-z0-9][a-z0-9\-]*[a-z]\.([a-z]{3,6}|(co\.)?[a-z]{2}))\.?(:[0-9]+)?$ [NC]
RewriteRule ^(.*)$ http://www.%2/$1 [R=301,L]

This code is for use in .htaccess, or in a server config file within a <Directory> section. For use in a config file and not enclosed in a <Directory> section, add a leading slash to the RewriteRule pattern, making it "^/(.*)$".

This code should properly handle all valid but non-canonical HTTP-only hostname requests, with the exception that it will not redirect in order to correct casing errors.

Jim