July 6th, 2007 Redirecting all ServerAliases to a preferred domain with mod_rewrite

I had a domain that I owned three TLDs for. I had the .com, the .net, and the .org. These all go to the same site using ServerAlias directives in the apache config. As do the www versions of each. However, I preferred to have them all go to mysite.com with no www. To add to the confusion, the site allows (but does not require) SSL, so the user’s SSL state needed to be preserved during all this rewriting. Here is what I came up with, and it works great.

 
<ifmodule>
RewriteEngine On
RewriteCond %{HTTP_HOST}    !^mysite\.com [NC]
RewriteCond %{HTTP_HOST}    !=""
RewriteCond %{SERVER_PORT}s ^(80s|443(s))$
RewriteRule ^(.*)          http%2://mysite.com/$1 [L,R=301]
</ifmodule>
 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Control Your Internet

Search