Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have two domains - oliverhaslam.com and ojhaslam.co.uk.

Both are registered with the same company, and both have A records pointing to my server's IP address.

Both also have CNAME records pointing the www address to the equivalent non-www address (www.oliverhaslam.com to oliverhaslam.com etc.)

The issue is this: while www.oliverhaslam.com redirects to oliverhaslam.com and displays that in the browser's address bar, www.ojhaslam.co.uk directs to the correct IP, but not to ojhaslam.co.uk.

Adding the serveralias of www.ojhaslam.co.uk to my virtualhosts makes the content display in a browser correctly, but the address bar does not reflect the redirection to ojhaslam.co.uk. As I say, that is not the behaviour I see with oliverhaslam.com, and I fear that SEO etc will see www.ojhaslam.co.uk and ojhaslam.co.uk as two separate sites.

Here's my httpd.conf file:

    <VirtualHost *:80>
ServerName oliverhaslam.com
ServerAlias oliverhaslam.com
DocumentRoot /var/www/
</VirtualHost>


<VirtualHost *:80>
ServerName ojhaslam.co.uk
ServerAlias ojhaslam.co.uk
DocumentRoot /var/www/photo365/
</VirtualHost>

Any ideas why having two domains, configured the same at the registrar and configured the same in httpd.conf are acting differently?

Thanks.

EDIT:

I now have this working by changing the above code to:

<VirtualHost *:80>
ServerName oliverhaslam.com
ServerAlias oliverhaslam.com
DocumentRoot /var/www/
</VirtualHost>


<VirtualHost *:80>
ServerName ojhaslam.co.uk
ServerAlias ojhaslam.co.uk
#RedirectMatch permanent /(.*) http://ojhaslam.co.uk/$1
DocumentRoot /var/www/photo365/
</VirtualHost>

<VirtualHost *:80>
ServerName www.ojhaslam.co.uk
ServerAlias www.ojhaslam.co.uk
RedirectMatch permanent /(.*) http://ojhaslam.co.uk/$1
DocumentRoot /var/www/photo365/
</VirtualHost>

The question still stands though - why was it working for one domain, but not the other?

share|improve this question
    
I now have this working by changing the above vhosts - see edit. –  Oliver Haslam Dec 30 '12 at 0:37

3 Answers 3

up vote 4 down vote accepted

A CNAME Record is not intended to redirect, but to imitate another record.

You need to tell the client to redirect. This can be achieved by either:

1) creating new vhosts for the www. names, and using a Redirect directive, like this:

<VirtualHost *:80>
ServerName www.ojhaslam.co.uk
...
Redirect ojhaslam.co.uk
</VirtualHost>

2) using mod_rewrite:

<VirtualHost *:80>
ServerName ojhaslam.co.uk
ServerAlias www.ojhaslam.co.uk
...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
</VirtualHost>
share|improve this answer
    
Thanks. Any idea why the redirect WAS working for the primary domain via just a CNAME, even though I apparently had it setup wrong? Cheers. –  Oliver Haslam Dec 30 '12 at 0:59

You've had enough replies on how to fix this in your httpd.conf, so I won't go into that anymore.

The question remains why it did work in the beginning.

What happened when you went to www.oliverhaslam.com? Your DNS server went into its records, saw it was a CNAME to oliverhaslam.com and served your browser with the A record for oliverhaslam.com, being something like 1.2.3.4.

Okay, let's rest that case for now, we'll come back to that later. Now www.ojhaslam.co.uk. There a same scenario played, where the DNS server also came back with the correct IP in the end, being 1.2.3.4.

Then the request comes to IP 1.2.3.4 into Apache. Apache gets a request on one of its IP's, sees that it's a name-based virtual host and tries to find the matching VirtualHost-block in your configuration. And what the f*$#, it can't find it! There is none matching www.oliverhaslam.com, nor www.ojhaslam.co.uk. So what does it do? It goes to the default virtual host. Which is, according to the documentation:

Now when a request arrives, the server will first check if it is using an IP address that matches the NameVirtualHost. If it is, then it will look at each section with a matching IP address and try to find one where the ServerName or ServerAlias matches the requested hostname. If it finds one, then it uses the configuration for that server. If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.

And there's your explanation. Both www-sites were served by the first VirtualHost-block, which was that of oliverhaslam.com.

share|improve this answer
1  
Your explanation of the Apache default server block is correct, but the DNS explanation I didn't understand. The browser doesn't know or care if the resource its trying to access is a A record or CNAME. The recursive DNS query is done by the resolver. –  Sameer Dec 30 '12 at 2:53
    
Yeah well I wasn't sure about that. I just wanted to make the complete story including the DNS story. I knew the Apache part and some global DNS-knowledge.. I'll change it in my post. Thanks :) –  bartlaarhoven Dec 30 '12 at 2:54
    
Thanks for that. I'm still fuzzy why www.oliverhaslam.com dropped the www when loaded, but www.ojhaslam.co.uk did not, though. As neither was specified, shouldn't they both have defaulted to oliverhaslam.com? –  Oliver Haslam Dec 30 '12 at 18:37
    
Well, if I visit www.oliverhaslam.com, I see in my browser log that I'm getting redirected to oliverhaslam.com by Wordpress. This is not something that has anything to do with Apache or the DNS; this is probably Wordpress behavior. As that completely depends on your settings in Wordpress, and I don't know anything about those, I can't explain this exactly. But the forwarding seems to be done by Wordpress, so look there instead of in Apache configs.. –  bartlaarhoven Dec 30 '12 at 19:40
    
Are you sure that Wordpress is handling that? –  Oliver Haslam Jan 1 '13 at 18:35

Your alias in your configuration has absolutely no use:

ServerName oliverhaslam.com
ServerAlias oliverhaslam.com

You're aliasing the primary server name. It's like saying: "Hi, my name is Oliver. But you can call me Oliver". It's duplicate and therefor not necessary. For SEO purposes, just rewrite your www domain to it's non-www equivalent instead, using these lines:

# Redirect www to non-www (SEO optimization)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.oliverhaslam\.com$ [NC]
RewriteRule ^/(.*)$ http://oliverhaslam.com/$1 [L,R=301]

And for your secondary domain:

# Redirect www to non-www (SEO optimization)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.ojhaslam\.co\.uk$ [NC]
RewriteRule ^/(.*)$ http://ojhaslam.co.uk/$1 [L,R=301]

That should take care of all the proper redirection (using a 301 redirect).

share|improve this answer
    
Thanks for the reply. Did you see my edit above? It is now working, does that look OK to you? Also, any ideas why the www redirecting was working for the primary domain via just a CNAME record, whereas the secondary was not? Cheers. –  Oliver Haslam Dec 30 '12 at 0:58
    
Please note that a CNAME record has nothing to do with webserver redirection. All it does is tell any system that does a lookup for the IP address of the hostname www.oliverhaslam.com or www.ojhaslam.co.uk to see the value of the non-www version instead. It's like a dictionary with synonyms where it just says "See entry X instead for the meaning of this word.", but it will be an IP in this case. –  Oldskool Dec 30 '12 at 1:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.