Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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]
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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]
share|improve this answer
    
Works like a charm! Thanks! –  SaltyNuts Mar 2 '11 at 16:13

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

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

share|improve this answer
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.