I'm running a web service that will allow people to create their own "instances" of my application, running under their own domain. These people will create an A record to forward a subdomain of their main domain to my server.

The problem is that my server runs everything under SSL. So in my configuration for port 80, I have the following:

<VirtualHost *:80>
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule /(.*) https://mydomain.com/$1 [R=301]

This has worked well to forward all requests from the http: to https: domain. But as I said, I now need to let any domain automatically forward to the secure version of itself.

Is there a rewrite rule that will let me take the incoming domain and rewrite it to the https version of same? So that the following matches would occur:

http://some.otherdomain.com -> https://some.otherdomain.com
http://evenanotherdomain.com -> https://evenanotherdomain.com

Thanks for your help! Apache mod_rewrite makes my brain hurt.


1 Answer 1


I'm pretty sure you can do this with straight mod_rewrite rules (using a rewriteCond on HTTP_HOST), but from a configuration-management standpoint it may be easier to take a different tack: I assume you're running each SSL site on its own IP address (because SNI support isn't quite universal yet, though getting close). In this case I would go for the easy solution and set up virtual hosts for each IP ( that forward to the SSL version exactly as you have above.

If you're relying on SNI (everyone is on one IP) you can do the same thing with a NameVirtualHost on port 80...

  • 1
    Great answer. First off, for reminding me that every SSL host needs its own IP address. This could prove sticky if we want to scale to hundreds or thousands of users. I'd also never heard of SNI, but my research indicates it's impractical given the propensity of Windows XP clients still hitting our server in gut-wrenching number. Looks like I need to do some additional thinking about this. Thanks for your help!
    – Aaron Vegh
    Feb 18, 2011 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.