apache - Redirect domain name

08
2014-07
  • user1032531

    I have two domain names pointing to my server. I want them both to serve identical HTML. I thought I would just do the following, however, it was unsuccessful. Note that I eventually will want ssl 443 to act similarly.

    How do I make any requests to mydomain.net display HTML from mydomain.com?

    Thank you

    <VirtualHost *:80>
      ServerName mydomain.net
      RewriteEngine on
      RewriteRule mydomain.net mydomain.com [PT]
    </VirtualHost>
    
    <VirtualHost *:80>
      ServerName mydomain.com
      ...
      a bunch of stuff
      ...
    </VirtualHost>
    
  • Answers
  • Tero Kilkanen

    Use this VirtualHost configuration instead of the one in your question:

    <VirtualHost *:80>
      ServerName mydomain.com
      ServerAlias mydomain.net
      ...
      a bunch of stuff
      ...
    </VirtualHost>
    

  • Related Question

    apache - Redirect all non www visitors to the www
  • Jason

    I keep getting variations of the following answer, and it doesn't work

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

    The reason this solution is no good is that if I go to

    alwaysromaing.com/site

    I'll never get be redirected to the WWW.alwaysroaming.com/site

    Can anyone provide the correct rewrite required here?


  • Related Answers
  • barryj

    I'd suggest this:

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

    The key part is the '/' after the '^' in the second line. The NC in means it'll work if someone types in mixed/upper case.

  • James Polley

    This is such a common question that it's part of the documentation for mod_rewrite - which you did read, right?

    Assuming you're running on port 80:

    RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*)         http://fully.qualified.domain.name/$1 [L,R]
    

    the documentation has another example in case you're not on port 80, and covers other common scenarios