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.

What I'd like apache to do is match every request for example.com and redirect to www.example.com. Any other hosts sent by a browser that are not on this domain should return a 404.

Here are my two vhosts:

example.com:

<VirtualHost *>
    ServerAdmin webmaster@example.com
    ServerName www.example.com
    ServerAlias *.example.com

    DocumentRoot /var/www/example.com/

    <Directory />
            Options FollowSymLinks
            AllowOverride All
    </Directory>

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

    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined
</VirtualHost>

000-default:

<VirtualHost *>
    Redirect 404 /
</VirtualHost>

What actually happens is that "www.example.com" is beeing served as it should, a host header of "test" is beeing 404'd as it should, while "example.com" is beeing 404'd, and it shouldn't.

It's like apache matches "www" to the ServerName directive, but the ServerAlias is beeing ignored, and a subdomain of "sub" matches the default vhost instead.

share|improve this question

migrated from stackoverflow.com Jun 23 '10 at 2:58

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

    
If I remove the 000-default vhost apache will correctly forward non-www subdomain requests to www, but will also serve any junk host with the example.com vhost instead of returning the 404 from the 000-default one –  Dude Jun 22 '10 at 23:25

3 Answers 3

<VirtualHost *>
   ServerName example.com
   Redirect permanent / http://www.example.com/
</VirtualHost>

Will handle fixing your www/non-www stuff.

Then for any other hosts, just point your default vhost to some empty documentroot (or some script that handles whatever custom 404 you want.

share|improve this answer
    
This will cause a redirect loop. Furthermore, it will not forward the requests to the www domain as the rewrite rule does, but just blindly redirect to the www domain. –  Dude Jun 22 '10 at 23:20
    
Sorry dude, you're wrong on both counts. Assuming there is another vhost for www.example.com (which does not redirect), there will be no loop. and example.com/asdf/123 will be redirected to www.example.com/asdf/123. –  tim Jun 22 '10 at 23:57

You need to add example.com to your ServerAlias line:

ServerAlias example.com *.example.com

And then rewrite your RewriteCond to be:

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

...because I don't think you need that first %{HTTP_HOST} line.

share|improve this answer

using

ServerName example.com
ServerAlias *.example.com

I get the redirect from non-www to www, but example.com alone returns a 404

share|improve this answer

Your Answer

 
discard

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