Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have two domains for a website (we're using Apache), example.com and example.org. The website responds to a wildcard subdomain for both of these. I want, however, to have the .com as the canonical domain. By that, I mean, I want to redirect (301) everything to the .com, but maintain the subdomain. For example.

example.org => example.com
sub.example.org => sub.example.com

I don't know enough about regular expressions to work out how to set this up. Can anyone point me in the right direction?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Try this:

<VirtualHost *:80>
    ServerName example.org
    ServerAlias *.example.org
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(.*\.)example\.org$
    RewriteRule ^/(.*)$ http://%1example.com/$1 [R=301,L]
</VirtualHost>

..then have your other vhost (the one serving the content for example.com) configured with ServerName example.com and ServerAlias *.example.com.

share|improve this answer
    
Thanks a lot, this worked perfectly. Even better, I think I understand how it works, too. I hate just copying and pasting stuff without actually comprehending what it's doing. –  Edd Morgan Aug 23 '12 at 13:15
    
@EddMorgan Great! Let me know if you'd like to see more explanation for the individual components of the config; this answer is kinda short on explanation by my standards, heh. –  Shane Madden Aug 23 '12 at 17:02

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.