Page tree
Skip to end of metadata
Go to start of metadata

We recently needed to move several hundred mail domains from standard http to SSL secured HTTP (HTTPS). To do so, we chose one domain and used mod_rewrite to redirect to that domain. To make things more complicated, we also wanted to strip off the first part of the domain name.

If you were doing this for just one domain (or a small number) then you should be using a simple Redirect in a VirtualHost to accomplish this. However, since we needed to do it for a large number of domains, mod_rewrite was necessary to capture the hostname in a dynamic fashion.

As an example, our users were visiting:

to access their webmail and

to access their mail administration interface.

Our software only requires the

section of the hostname, and so we needed to redirect to


Our rewrite rules look something like this:

  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^(web)?mail\.(.*) [NC]
  RewriteRule ^ https://new_ssl_host/v/%2 [R=301,L,QSA]

  RewriteCond %{HTTP_HOST} ^(mail|site-)?admin\.(.*) [NC]
  RewriteRule ^ https://new_ssl_host/v/%2/admin [R=301,L,QSA]

On the SSL virtual host, we then pick up the hostname with the following rewrites:

RewriteEngine On
RewriteRule ^/v/([^\/]+)/admin(.*) /path/to/mail/admin/interface/public_html/$2 [L,E=SCHOOL_HOST:$1]
RewriteRule ^/v/([^\/]+)(.*) /path/to/our/webmail/interface/$2 [E=SCHOOL_HOST:$1]

Hope someone finds this useful, it took me a few hours to find the correct documentation to obtain the HTTP_HOST to use in the RewriteRule.

  • No labels