I'm sure this has been asked before but none of the answers seem to work for me.

I have a domain domain.com. I have a subdomain sub.domain.com on the same IP. In this case sub.domain.com is a CNAME record pointing to domain.com. I have apache running on this server. I have a mod_rewrite rule in sites-available/default that redirects all non-ssl connections to ssl:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

So far this works fine. If I connect to http://domain.com/foo I get directed to https://domain.com/foo with working ssl.

However, now I have a second webserver running on this server on a different port that I want to make accessible through sub.domain.com. I have created a CNAME record pointing sub.domain.com to domain.com so both have the same IP address. I can make an ssl connection to the second server at https://domain.com:port and it works fine.

As far as I can tell the mod_rewrite stuff above shouldn't cause any problems because if I connect to http://sub.domain.com I should first get redirected to https://sub.domain.com and then that should get proxied through to the second webserver.

Every blog post tutorial and serverfault answer I've followed has left me with configurations that either send everything to domain.com or give me ssl handshake errors when I try to connect to either sub.domain.com or domain.com.

Can anyone show me the correct way to do this and explain why it works?

  • Have you tried adding the [R] flag to the end of your RewriteRule? – Regan Nov 6 '13 at 14:06

I assume that your subdomain isn't in the same virtual host that your domain is. In this case, adding the rewrite rule to the default vhost will have no effect at all; you need to set the rewrite rule in the vhost configuration file for your subdomain as well (it should also be in sites-available if that is where your default one is, though be aware that this is a debianism and not all apache installations you encounter will be like that).

The lines you have for the RewriteEngine configuration are correct and should work if copied into the other file as well.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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