1

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.

  • 1
    superuser is for computer software and hardware. This question is not within that scope. I suggest serverfault. – Josh K Mar 22 '10 at 17:08
7

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> 
  • 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" – cpuguru Mar 22 '10 at 17:07
  • @cpuguru see my edit above – Matt Mar 22 '10 at 17:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.