Serve both www. and bare hostname - best practice


(Tom Chadwin) #1

We have a multiple site installation, in the process of migrating to v5. Many of our sites serve both www.site.com and site.com by having both as site URLs. This leads to two issues:

 

1. When linking between Matrix sites, a random (and hence inconsistent) URL is chosen

 

2. Therefore, Google et al spider both addresses, which we obviously Do Not Want

 

What is the best way around what must be a fairly common issue? Ideally we want to force one of the above always to redirect to the other, but always to the full path requested, eg if site.com/path/to/page is requested, the request is redirected to www.site.com/path/to/page throughout. Can this be done with eg rewriting by Apache?

 

Thanks

 

Tom


(Nic Hubbard) #2

Apache.

 

Put the following above any Matrix Alias lines:

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

So if a user visits zedsaid.com, Apache automatically rewrites this to www.zedsaid.com before Matrix even sees it. Therefore you only need one URL per site in Matrix.

 

Then just add additional RewriteCond and RewriteRule for additional sites.


(Tbaatar) #3

Nic,

 

Do you know how to reverse the redirect? so www.domain.com redirects to domain.com

 

 

Thanks.


(Nic Hubbard) #4

Nic,

 

Do you know how to reverse the redirect? so www.domain.com redirects to domain.com

 

 

Thanks.

 

Yeah, just reverse it:

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

#5

We had the same issue however we had to serve two urls as some sites were still on our older publishing server and some were via matrix.

Redirects on apache is how we deal with the double urls but we are slowly …cleaning them up