I think that I have this sorted (thanks mainly to the question How to redirect non-www to www without hardcoding using .htaccess?), but I still don't entirely understand a couple of things.
I would like to force all non-SSL connections to my server to be routed to SSL. I have only one vhost (and that will reliably remain the case for the lifetime of the server), but I would like to avoid hard-coding the domain name, partly so that the httpd.conf files for Staging and Production remain identical.
I know I can force requests to use SSL with a mod_rewrite rule like
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
or
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
I have two relatively simple questions, though:
- Should I prefer one of those
RewriteCondstatements over the other for any reason? Presumably if I use the%{SERVER_PORT}variable, any connections on, say, port 8000 would continue to be served in the clear? Is there some reason I should avoid using the%{HTTPS}variable that I'm overlooking? - Will that
%{HTTP_HOST}variable in theRewriteRulestatement be respected and automagically substitute in whatever theHost:header was from the request? Is there some circumstance under which this might not work?
In case it makes a difference, we're running Apache 2 on RedHat with mod_ssl and the site used Drupal 7.
Sorry for what is a relatively stupid question; Apache sysadmin is by no means a core part of my job, so I'm trying to muddle through as best I can. Thanks, all!
%{HTTPS}, are there's no guarantee that your non-ssl is being served over 80, which confirms my suspicion. He also mentioned "afaicrhttp_hostcomes from the inbound request indeed": twitter.com/jaymzcampbell/statuses/158980755693912064 – Owen Blacker Jan 16 at 20:50Host:request header. This would be rather rare, though, to say the least. Beyond that, I can’t think of a scenario where it would occur. – Mo. Jan 17 at 13:38HTTP/1.0, not sending aHost:header. Should I worry about it, in your opinion? – Owen Blacker Jan 17 at 13:53