Apache redirect from one domain to another domain

8,650

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> 
Share:
8,650

Related videos on Youtube

cpuguru
Author by

cpuguru

Science &amp; Technology Aficionado

Updated on September 17, 2022

Comments

  • cpuguru
    cpuguru almost 2 years

    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.

    • Josh K
      Josh K over 14 years
      superuser is for computer software and hardware. This question is not within that scope. I suggest serverfault.
  • cpuguru
    cpuguru over 14 years
    Thx for the reply. My weak understanding leads me to believe that the user would still be looking at "foo.net" in their browser (even though looking at the same files as "foo.com") and that the logs would reflect that if I did this. I'd like to redirect the user to "foo.com" if possible so that their bookmarks and our logs always reflect a visit to "foo.com"
  • Matt
    Matt over 14 years
    @cpuguru see my edit above