Differences between revisions 5 and 6
Revision 5 as of 2009-09-30 13:40:12
Size: 1857
Editor: RichBowen
Comment: Altered comment, based on greater understanding of the situation, and discussion with the original author.
Revision 6 as of 2009-09-30 13:41:50
Size: 1781
Editor: RichBowen
Comment: Gah! I *still* misread the rules. I shouldn't edit wikis before my morning coffee. :/
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Note: The rules described here are intended to go in a .htaccess file.
Line 8: Line 6:
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. 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.

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:

  http://webmail.school.county.sch.uk/

to access their webmail and

  http://admin.school.county.sch.uk/

to access their mail administration interface.

Our software only requires the

  school.county.sch.uk

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

  https://new_ssl_host/v/school.county.sch.uk/webmail
  https://new_ssl_host/v/school.county.sch.uk/admin

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.

RewriteDomainToPath (last edited 2009-09-30 13:41:50 by RichBowen)