Apache Server – Redirect One Domain to Another

apache-http-serverredirection

Like many users, we tend to register the *.com and *.net versions of our domain names to prevent nefarious squatters. So if we wanted "foo.com" we'd also register "foo.net" and have them both resolve to the same IP address.

I'm trying to set up Apache for the first time and need to know the proper way to redirect requests to "foo.net" to go to "foo.com" instead so that if a user types in "foo.net" they get magically redirected to "foo.com".

I've been reading through the Apache URL Rewriting guide and it's not readily apparent how to do this seemingly simple task.

Best Answer

You don't need to rewrite this.. just add another vhost that points to the same DocumentRoot, e.g.:

<VirtualHost *:80>
        DocumentRoot "/var/www/yoursite.com"
        ServerName yoursite.com
</VirtualHost>

<VirtualHost *:80>
        DocumentRoot "/var/www/yoursite.com"
        ServerName yoursite.net
</VirtualHost>

If you're unfamiliar with vhosts, you might want to read about them here.

EDIT:

In response to OP's comment:

I understand what you want now. What you're looking for is a ServerAlias redirect. So, in your vhost, you can add something like:

<VirtualHost *:80> 
    ServerAlias yoursite.net
    redirect permanent / http://yoursite.com
</VirtualHost> 
Related Question