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.

When I enter in my domain like this

www.domain.com

everything works fine. But when I enter it like this

domain.com

I get the 'It works!' page. I'm a complete Apache newb so I'm not exactly sure whats going wrong here.

share|improve this question

migrated from stackoverflow.com Dec 14 '09 at 1:49

This question came from our site for professional and enthusiast programmers.

4 Answers 4

Traditional solution: in the httpd configuration, find:

ServerName www.domain.com

and add:

ServerAlias domain.com

However, this will give you a server that responds with the same content on domain.com and www.domain.com. It's generally considered preferable (particularly SEO-wise) to have only one canonical hostname. To do that, add a new virtual server for the redirect, instead of the alias.

<VirtualHost *:80>
    ServerName domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>
share|improve this answer

This has to do with the Apache config as others have stated.

Simply added a ServerAlias to the apache config for your virtualhost will do it for you. I don't think that it is good practice to create another virtualhost have it redirect for you.

It is good SEO practice to direct everything to one or the other, I suggest using this in either you config file or in your .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
share|improve this answer
    
Why do you not think it is good practice to have a virtualhost do the redirect? It is simpler and has less performance impact than processing everything with mod_rewrite. –  bobince Dec 14 '09 at 3:49
    
I think it is partly to do with preference, but if you have to manage hundreds of virtualhosts, it can become cumbersome to keep all these 'stub' virtualhost up to date and organized. I think there use to be a problem with apache loading up a lot of virtualhost, not sure if this is a problem with apache still. If you are just hosting one or two sites it isn't too big of a deal. –  Ryan Gibbons Dec 14 '09 at 14:34

I had this problem too, and found my solution here.

Basically, after an upgrade, Apache put a symlink in /etc/apache2/sites-available. Deleting everything between and including <VirtualHost *:80> and <VirtualHost> got everything working for me again.

share|improve this answer

Check the mod_alias at your apache config file, make sure that the regex is correct for redirection.

share|improve this answer

Your Answer

 
discard

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