Welcome to Server Circle. It's a friendly site and all levels of experience are welcome. Be aware that we use cookies for your login.
Server Circle - Ask questions about Servers and get answers from experts.
Beta (0.36 sec)
Apache mod_rewrite not working

I have this in my default HTTP vhost:

rewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]

RewriteRule ^(.*)$ http://domain.com [L,R=301]


It's shifts www.domain.com to just domain.com.

How can I do that in my default HTTPS vhost file ?
Asked by:
twosongs
920 points
 Report Abuse
 Share Page - Category: Linux Servers - Tags: Apache mod_rewrite not working
 Enter your response
Please use Pastie.org to paste lengthy code or to fix formatting issues with code
  • Responses in reverse (3)

Best Answer

I doubt it would be very useful... your SSL certificate will have a CN set to a specific hostname (such as secure.example.com), if you use redirects on it at all, you will get a certificate error at one of the stages.

Say you want to redirect https://secure.example.com to https://example.com - The SSL certificate will be for secure.example.com, which will validate ok when the client first connects, but when the client makes the connection for the redirected site, the certificate will not verify due to the hostname being different.

Of course, if you are using self-signed certificates, you'll get an error regardless.

If you did want to do it anyway, the procedure is exactly the same as it is for standard http, only replace the http:// with https://

If you wanted to make sure that the visitor was using SSL, then you could do something like this:



RewriteEngine On

RewriteCond %{HTTPS} !^on [NC]

RewriteRule ^(.*)$ https://secure.domain.com/$1 [L,R=301]


Response by:
HelpQuick
266 points
Had a look around and not found an absolute answer, but with ref to the previous comment, I don't think there is a HTTPS_HOST (not mentioned in Apache docs), so at a guess HTTP_HOST would still work there.

Other comment about changing to https:// would seem valid however.

Just one thing however - would there really be any advantage in doing this switch to the non-www version of the https? Can't think of any advantages personally, but might be of course in this case.

Response by:
trevHCS
640 points
Just changing HTTP_HOST to HTTPS_HOST and http:// to https:// might help.

Response by:
Trinity
2782 points


  • Related Questions
About Us : Contact Us : Etiquette : Terms : CDN Failover : ShorterURL : CDN Fallback : © 2013 Server Circle