Did you know?
In Apache rewrite rules, %{HTTP_HOST} keeps the port: Host example.com:80 becomes https://example.com:80/.
Build HTTPS redirects with %{SERVER_NAME}, not %{HTTP_HOST}, or use a fixed hostname.
- Web servers
- Applies to Apache 2.4
- Last checked
Explained
%{HTTP_HOST} is the raw Host header, port included. An HTTPS redirect built from it sends a client that sent Host: example.com:80 to https://example.com:80/, which tries TLS on port 80 and fails. %{SERVER_NAME} drops the port:
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Both still follow the client’s hostname, so add a catch-all default vhost, or put a fixed hostname in the target.
Learn more
- How to Redirect HTTP to HTTPS in Apache (Secure Setup) Full guide · Updated Nov 2026
