Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know there are already several posts asking about redirecting webpages but I can not seem to find exactly what I am looking for.

I have a website (for example www.example.com) that needs to be redirected:

  1. example.com
  2. www.example.net
  3. example.net

Should all be redirected to www.example.com

I found Apache redirect from one domain to another domain but I am not sure how to add it to my file located in /etc/apache2/sites-available This is what the file currently looks like:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example
    ServerName www.example.com
    ErrorLog logs/www.example.com-error_log
    TransferLog logs/www.example.com-access_log
</VirtualHost>

Any help would be appreciated.

Thanks

share|improve this question
    
Do you need a redirect (HTTP code 301/302)? If it is enough that all domain names deliver the same content, you can just add a ServerAlias example.com www.example.net example.net inside the VirtualHost statement. –  Stefan Seidel Sep 17 '12 at 13:56
add comment

1 Answer

up vote 3 down vote accepted

You just add them one after the other. Example: http://httpd.apache.org/docs/2.2/vhosts/examples.html

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example
    ServerName www.example.com
    ErrorLog logs/www.example.com-error_log
    TransferLog logs/www.example.com-access_log
</VirtualHost>

<VirtualHost *:80> 
    ServerAlias example.net
    redirect permanent / http://www.example.com
</VirtualHost> 

share|improve this answer
    
I'm not sure you can have a VirtualHost without ServerName, so that ServerAlias in the second VirtualHost block should rather be ServerName. –  Stefan Seidel Sep 17 '12 at 13:55
2  
Link -> A ServerName should be specified inside each <VirtualHost> block. If it is absent, the ServerName from the "main" server configuration will be inherited.) Depends on setup, but he probably needs it. Good catch. –  Zero Sep 17 '12 at 14:00
    
There may be some DNS CNAME configuration in order here as well. –  JoshP Sep 17 '12 at 18:24
add comment

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.