Canonicalize Apache Traffic: HTTPS and www Redirects with mod_rewrite
Redirect HTTP to HTTPS and non-www to www in Apache 2.4 virtual hosts using mod_rewrite, with reverse-proxy safeguards.
21 May 2026, 04:31 UTC

One site, four addresses
Your application is reachable at http://example.com, https://example.com, http://www.example.com, and https://www.example.com. Users bookmark whichever they type first. Search engines index duplicates. Cookies and sessions split across hosts. Browsers flag mixed content when assets load over HTTP while the page is HTTPS. The useful takeaway: canonicalize early, at the edge, so every request lands on one scheme and host before it reaches your application.
Why put the rules in the virtual host
mod_rewrite is Apache's rule-based rewriting module. In Apache 2.4, you can write rules in a virtual host container or in per-directory .htaccess files. A .htaccess file is re-read and re-evaluated on every request, which adds filesystem stat overhead, and its rules inherit and combine with virtual host rules in ways that make redirect loops harder to trace. For a canonicalization policy that applies to the whole site, a virtual host Directory or VirtualHost block is the clearer, faster place.
Two directives do most of the work. RewriteCond tests a condition such as the request scheme or the Host header. RewriteRule rewrites the URI or issues a redirect. The flag [R=301,L] sends a permanent 301 redirect and stops processing further rules for that request.
Worked configuration
Assumptions: Apache 2.4, mod_rewrite loaded, root or sudo access to the server config, and you are editing the virtual host for example.com. Replace example.com with your actual domain.
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
# Force www on HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
</VirtualHost>
Place this in the site's virtual host file, commonly under /etc/apache2/sites-available/ on Debian-derived systems or /etc/httpd/conf.d/ on Red Hat-derived systems. Run apachectl configtest as root or with sudo before reloading. If the test passes, reload Apache with systemctl reload apache2 or systemctl reload httpd, depending on your distribution.
Risk: omitting L lets later rules keep processing after a redirect, which can produce loops. Conflicting rules in .htaccess under the same document root can also override or duplicate this logic.
Behind a reverse proxy or load balancer
If Apache sits behind a TLS-terminating load balancer, %{HTTPS} may read off even when the client connected over HTTPS, because Apache itself receives plain HTTP from the balancer. A rule that redirects whenever %{HTTPS} is off can then send the client back to HTTP, and the balancer sends it back to Apache: a loop.
Check for the proxy header instead:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Only use this form when you trust the proxy not to forge X-Forwarded-Proto. If clients can send that header directly, they can bypass the redirect.
Verify and check limits
- Confirm the module is loaded:
apachectl -M | grep rewrite. Expect a line containingrewrite_module. - Test the redirect:
curl -I http://example.com/. ExpectHTTP/1.1 301and aLocationheader beginning withhttps://www.example.com. - Test the www canonicalization:
curl -I https://example.com/. Expect301tohttps://www.example.com/. - Behind a proxy, send
curl -I -H "X-Forwarded-Proto: http" https://www.example.com/and confirm you still get a redirect to HTTPS, not a loop back to HTTP.
Limitations: mod_rewrite rewrites the request URI; it does not validate TLS certificates, enforce HSTS, or block application-layer attacks. Use HSTS headers separately if you want browsers to remember the HTTPS-only policy. Also, these rules canonicalize the host and scheme but do not normalize trailing slashes or case.
Rollback
Because reloading Apache changes running state, keep a copy of the previous virtual host file. To roll back, restore the backup, run apachectl configtest, and reload Apache again.
Closing
Canonicalizing at the Apache edge keeps your application from seeing duplicate schemes and hosts. Put the rules in the virtual host, test with configtest and curl, and switch to X-Forwarded-Proto when a load balancer terminates TLS.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.