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>

Related Posts:

About AaronCampbell
Aaron is the man behind the curtain here at Xavisys. He gets things done and really knows his way around the code.

Comments

One Response to “Redirecting all ServerAliases to a preferred domain with mod_rewrite”
  1. Y920.com says:

    ServerAlias directives in the apache config is one of the big source,

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!