# Force SSL with Name-based Virtual Host (possibly with .htaccess)

I have several subdomains served by Apache using Name-based Virtual Hosts. The way it's set up is that I have one directory holding all the site's pages, but it's accessible using several subdomains (a web application with a different theme for each customer). For each subdomain I have a symlink to the main dir, which matches the name-based virtual host mapping. So I just use $_SERVER['HTTP_HOST'] in my php scripts to determine which theme to serve, but the code base is the same. The problem is that the sites need to be SSL and I have trouble figuring out how to force it that way. I have seen this example for .htaccess: RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]  But the trouble is that it requires you to hard code the domain www.example.com. How can I make it go to SSL version of whatever domain is actually requested, instead of hard coding it? I have seen {HTTP_HOST} variable getting used in RewriteCond portion of .htaccess, but it does not appear to work when I attempt to use it in RewriteRule. Tried the following and it gave me a 500 error: RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://{HTTP_HOST}/$1 [R,L]  ## 2 Answers The ability to use those same conditional variables in the substitution string makes this a simple fix. Change RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]  to RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://%{HTTP_HOST}/\$1 [R,L]


Apache SSL dont work with name virtual host.. see:

http://wiki.apache.org/httpd/NameBasedSSLVHostsWithSNI

• This is a poor answer, your link shows exactly how Apache DOES work with named virtual hosts using SNI. – gparent May 9 '12 at 15:57