The issue happens when you:
- issue a request with the header "Host" including the port, e.g. "Host: www.example.com:80", which is legal as per https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23. You can do it for instance with curl
curl -v -H "Host: www.example.com:80" -X GET -i http://www.example.com - the server issues a redirect to https for that request, in my case using the following RewriteRule
RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] I noticed that the "Location" header of the response also includes the port, and it's the same of that specified in the "Host" header of the request. So the server would respond with "Location: https://www.example.com:80", which is wrong.
This happens to me with "Apache/2.4.7 (Ubuntu)", but I noticed the issue also with Varnish cache server. Why does it behave this way? Is there a way to correct this?
RewriteRulefor this? I find it easier to simply have separate<VirtualHost>sections for HTTP and HTTPS. In that case the HTTP section only needs to use aRedirectand it can be trimmed down a lot. And why are you using theHostheader to find out where to redirect to? You could end up redirecting to a domain for which you don't have a certificate. I would instead write an actual domain name in theRedirectdirective such that it is guaranteed to match the certificate.Hostheader from the client. Why? I assume you know the domain name of your server. Why not write the domain name in aRedirectdirective instead of relying on the client to tell you? I have done it like thisRedirect / https://example.com/which has worked without any problems.%{HTTP_HOST}. With the redirect you say, the problem doesn't happen. Why have I been using this? Because I found it out years ago answers with many points on stackoverflow, for instance in stackoverflow.com/a/3239775/1504300 and stackoverflow.com/a/4083233/1504300. Now that I see it, my question is pretty obvious, but I think you should answer "officially", then I would point this entire question in those 2 mentioned as "why not".