How to Redirect HTTP to HTTPS on Apache 2.4 (Ubuntu, Debian, RHEL 9 Family)
Send all HTTP traffic to HTTPS on Apache 2.4 using a virtual host Redirect or a .htaccess rule, check it with curl, and decide whether to add HSTS.
Once a site has a working TLS certificate, plain HTTP should do one thing only: send the visitor to the HTTPS address. This guide sets that up on Apache 2.4, first in the virtual host configuration (the method the Apache documentation prefers) and then in .htaccess for accounts without access to the server configuration. It applies to Ubuntu 26.04 LTS and 24.04 LTS and Debian 13 and 12, where the service is called apache2, and to AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9, where it is called httpd.
Requirements
- Apache 2.4 with a virtual host that already serves the site correctly on
https://example.com/. Open that address first; a redirect to a broken HTTPS site only makes the whole site unreachable. - Ports 80 and 443 open in the firewall.
- A user with
sudorights, or for the.htaccessmethod, write access to the site's document root.
Method 1: Redirect in the port 80 virtual host
Edit the site's configuration file. On Ubuntu and Debian it is in /etc/apache2/sites-available/ (for example example.com.conf); on the RHEL family it is usually a file in /etc/httpd/conf.d/. Reduce the port 80 virtual host to a name and a redirect, and leave the port 443 virtual host as it is:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent "/" "https://example.com/"
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
# existing SSL and site configuration stays here
</VirtualHost>
Redirect belongs to mod_alias, which is part of a standard Apache installation, so there is no module to enable. It matches every request whose path starts with / and appends the rest of the path and the query string to the target: http://example.com/shop/item?id=7 goes to https://example.com/shop/item?id=7. Keep the trailing slash on the target, otherwise the appended path is glued straight onto the host name.
In this example requests for www.example.com are also sent to https://example.com/. If each name should keep its own host name, write one port 80 virtual host per name, each with its own target.
Method 2: Redirect in .htaccess
On shared hosting you often cannot edit virtual hosts. In that case put these lines at the top of the .htaccess file in the document root:
RewriteEngine On
RewriteCond "%{HTTPS}" !=on
RewriteRule "^(.*)" "https://%{SERVER_NAME}/$1" [R=301,L]
This needs mod_rewrite and a virtual host that allows overrides (AllowOverride FileInfo or All for the directory). Check whether the module is loaded:
sudo apachectl -M | grep rewrite
If nothing is printed on Ubuntu or Debian, enable it and restart Apache:
sudo a2enmod rewrite
sudo systemctl restart apache2
Use this method only when you have to. A .htaccess file is read on every request, and a rule placed there is easy to break when an application rewrites the file.
When TLS ends at a proxy or load balancer
%{HTTPS} reports what Apache itself sees. If a load balancer or CDN terminates TLS and talks plain HTTP to Apache, the variable is always off and the rule above loops forever. Test the header the proxy sets instead:
RewriteEngine On
RewriteCond "%{HTTP:X-Forwarded-Proto}" =http [NC]
RewriteRule "^(.*)" "https://%{SERVER_NAME}/$1" [R=301,L]
Rely on this header only if every request reaches Apache through your proxy; a client that can connect directly can send any value it likes.
Permanent or temporary
Without a status, Redirect sends a temporary redirect (302). Redirect permanent sends 301, which tells search engines to replace the HTTP address and lets browsers cache the answer. Because of that caching, start with Redirect temp (or R=302 in the rewrite rule), confirm that everything works, and then switch to the permanent form.
Apply and test
Check the configuration and reload Apache. On Ubuntu and Debian:
sudo apachectl configtest
sudo systemctl reload apache2
On AlmaLinux, Rocky Linux and CentOS Stream:
sudo apachectl configtest
sudo systemctl reload httpd
A .htaccess change needs no reload. Now request an HTTP address with a path and a query string and look at the headers only:
curl -I "http://example.com/shop/item?id=7"
You should see the status and the HTTPS target:
HTTP/1.1 301 Moved Permanently
Location: https://example.com/shop/item?id=7
Then confirm that the HTTPS address itself answers 200:
curl -I "https://example.com/shop/item?id=7"
Optional: add HSTS after the redirect works
A redirect still lets the very first request travel unencrypted. The Strict-Transport-Security header tells a browser that has visited the site once to use HTTPS on its own from then on. Add it to the port 443 virtual host only, because browsers ignore it when it arrives over HTTP:
Header always set Strict-Transport-Security "max-age=300"
The directive needs mod_headers (sudo a2enmod headers on Ubuntu and Debian, followed by a restart). Start with a few minutes as shown, and raise the value to a year (31536000) only when you are sure the site will stay on HTTPS: until the period runs out, browsers refuse to load the site over HTTP and do not let visitors click past certificate errors. Add includeSubDomains only if every subdomain has a valid certificate.
Frequently asked questions
Does the redirect break Let's Encrypt renewals?
No. The HTTP-01 validation request arrives on port 80 and follows redirects to HTTPS on port 443, so a blanket redirect is fine as long as the challenge path is still served on the HTTPS side.
What happens to POST requests?
A redirect does not carry the submitted body along, and after a 301 or 302 browsers normally repeat the request as GET. Forms and API clients should therefore use the https:// address directly instead of depending on the redirect.
Should I use Redirect or mod_rewrite?
Use Redirect whenever you can edit the virtual host. It is one line, has no pattern to get wrong and is read once when Apache starts rather than on every request. Keep mod_rewrite for .htaccess and for conditions that Redirect cannot express, such as the forwarded-protocol check.
Why do I get "too many redirects"?
Either the redirect was placed in the port 443 virtual host (or outside any virtual host) so HTTPS requests are redirected too, or TLS is terminated in front of Apache and the rule tests %{HTTPS}. Move the directive into the port 80 virtual host, or use the forwarded-protocol variant.