0

I own two domain names:

  • domain.com
  • domain.net

…and I can't determine how to best setup the vhost code in order to achieve a few things:

  1. If a visitor types in either of those two domain names, they go to domain.net
  2. If a visitor types in either of those two domain names with www in front, they still go to domain.net (without www)
  3. Keep file paths in place, so:
    • domain.com/file.php ends up going to domain.net/file.php
    • www.domain.com/deeper/path/ ends up going to domain.net/deeper/path/
    • www.domain.net ends up going to domain.net
    • …and so on

I saw, in this Q/A entry, what I believe is what I need, but I'm not 100% certain. Can somebody confirm (or correct) that this'll do exactly what I need:

<VirtualHost *:80>
    ServerName  domain.net
    ServerAlias www.domain.net domain.com www.domain.com
    (the rest of the settings here)
</VirtualHost>

…is it as simple as that?

Community
  • 1
  • 1
Marc Amos
  • 137
  • 2
  • 11

1 Answers1

3

As is, your vhost will accept your aliases and respond with the correct files but won't redirect the requests. If you want a canonical sub domain, you may want to add a few rewrite rules:

<VirtualHost *:80>
    ServerName  domain.net
    ServerAlias www.domain.net domain.com www.domain.com

    RewriteEngine On
    RewriteCond %{HTTP_HOST}    !^domain\.net [NC]
    RewriteCond %{HTTP_HOST}    !^$
    RewriteRule ^/(.*)  http://domain.net/$1 [L,R=301]

    (the rest of the settings here)
</VirtualHost>

Check http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html#canonicalhost for further explanations

nikoshr
  • 32,926
  • 33
  • 91
  • 105