To redirect HTTP to HTTPS in Apache, add a port 80 virtual host that sends every request to the same address on HTTPS with a permanent redirect: Redirect permanent / https://example.com/. This works without mod_rewrite and keeps the path and query string intact. You need a working TLS certificate on port 443 first, otherwise visitors land on a certificate error.
Forcing HTTPS protects logins and cookies, avoids browser "not secure" warnings and is expected by search engines. This guide shows the simple Redirect method, the mod_rewrite alternative, and how to avoid redirect loops.
Check these first:
<VirtualHost *:443> block.ssl module enabled: sudo a2enmod ssl.curl -I https://example.com/ before adding the redirect.If HTTPS is broken and you redirect anyway, every visitor is sent to a failing page. Always test HTTPS first.
This is the recommended method because it is simple and fast. Port 80 gets its own virtual host whose only job is to redirect.
Redirect permanent sends a 301 status. A request for http://www.example.com/blog?id=4 goes to https://example.com/blog?id=4: Redirect appends the rest of the path and the query string automatically.
Apply it:
Expected output:
If one virtual host serves many domains, use mod_rewrite so the target keeps the requested host:
Enable the module with sudo a2enmod rewrite first. %{REQUEST_URI} contains the path, and the query string is carried over by default.
When you cannot create a separate port 80 host (shared hosting, a single combined config), put a condition in .htaccess or the main config:
The condition %{HTTPS} off matches only plain HTTP requests, so HTTPS traffic is not redirected again. In .htaccess, the directory must allow it with AllowOverride FileInfo (or All). The virtual host method is faster because Apache does not read .htaccess on every request.
If a load balancer terminates TLS and talks plain HTTP to Apache, %{HTTPS} is always off on the backend, and the rule above causes an infinite loop. Test the forwarded header instead:
Only trust X-Forwarded-Proto when it is set by your own proxy, never by clients on the public internet. Better still, let the load balancer do the redirect itself.
Use 301 once you are sure HTTPS will stay. Browsers and search engines cache it hard. While testing, use a temporary redirect so a mistake can be undone:
After the redirect works, add HTTP Strict Transport Security to the HTTPS host so browsers go to HTTPS on their own next time:
Start with a short max-age, for example 300 seconds, and raise it once everything works. Do not add includeSubDomains or preload until every subdomain supports HTTPS, because they are hard to undo.
| Symptom | Cause | Fix |
|---|---|---|
| Browser says "too many redirects" | TLS ended at a proxy and Apache sees HTTP | Check X-Forwarded-Proto instead of %{HTTPS} |
| Redirect goes to the wrong host | Fixed host in the rule | Use %{HTTP_HOST} or one canonical ServerName |
| Redirect is ignored | The request matches a different virtual host | Run apache2ctl -S to see which host answers |
| Certificate warning after redirect | Certificate does not cover the name | Reissue the certificate with all names |
| Form posts lose data | 301 can turn a POST into a GET | Redirect pages, and fix links to use HTTPS |
A dedicated port 80 host with Redirect permanent is simpler and faster. Use RewriteRule when you need the requested hostname or a condition.
Yes. Both Redirect and the rewrite examples above preserve the path and query string.
No. A single 301 from HTTP to HTTPS is the standard approach and search engines treat HTTPS as the canonical version. Avoid chains of several redirects.
A graceful reload with systemctl reload apache2 applies the change without dropping connections.
Test HTTPS first, then add a port 80 virtual host with Redirect permanent / https://example.com/. Use mod_rewrite with RewriteCond %{HTTPS} off when a combined config is unavoidable, check the forwarded protocol header behind proxies, and add HSTS only after the redirect is proven.