Home / Fix guides / HTTP not redirecting to HTTPS / Apache

Apache fix guide

How to redirect HTTP to HTTPS on Apache

If visitors can still open the plain http:// version of your site, Apache is serving it without sending them to https://. This page shows the safest way to add a permanent redirect and how to check it worked.

Apache does not redirect HTTP to HTTPS on its own. Even with a valid certificate installed, port 80 and port 443 are treated as two separate sites, and you have to tell the port 80 one to send people to the secure address. The fix takes a few lines of configuration and a reload.

Before you start

The redirect only helps if HTTPS already works. Open https://example.com/ (use your own domain) and check that the page loads with no certificate warning. If it does not, install a certificate first. Certbot with the Let's Encrypt service is a common free option, and running certbot --apache can set up the certificate and the redirect for you. If your host uses cPanel or a similar panel, look for a certificate or AutoSSL tool and a "Force HTTPS Redirect" switch in the domain settings. Names differ between panels.

Also decide on one preferred address, for example https://example.com/ or https://www.example.com/. Redirect everything to that one address so you do not create a chain of redirects.

Step by step

  1. Check the current behaviour. From a terminal, run:
curl -I http://example.com/

If you see 200 OK instead of 301 Moved Permanently with a Location: https://... line, the redirect is missing.

  1. Find the right file. On a server you control, the port 80 settings live in a virtual host file. On Debian and Ubuntu this is usually in /etc/apache2/sites-available/ (often 000-default.conf or yourdomain.conf). On RHEL, Rocky and AlmaLinux look in /etc/httpd/conf.d/. On shared hosting you usually cannot edit these, so use .htaccess in your site root instead (step 4).
  1. Add the redirect to the port 80 virtual host (preferred). This is the cleanest method and needs no extra modules:
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>

Replace the domain with yours. Redirect keeps the rest of the path, so /shop/ goes to https://example.com/shop/. Make sure your existing <VirtualHost *:443> block has the certificate settings and your site content.

  1. Or use .htaccess (shared hosting). This needs mod_rewrite enabled and AllowOverride set to allow rewrite rules (FileInfo or All) for your site folder. Many shared hosts already allow this. Add near the top of .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

If you use WordPress, put these lines above the # BEGIN WordPress block so they run first. Always keep a copy of the original file before editing.

  1. Test the configuration, then reload. On Debian or Ubuntu:
sudo apachectl configtest
sudo systemctl reload apache2

On RHEL based systems the service is called httpd. If you used .htaccess only, no reload is needed. If configtest reports an invalid RewriteEngine command, enable the module with sudo a2enmod rewrite (Debian and Ubuntu) and reload.

  1. Test again. Run curl -I http://example.com/ and confirm you get 301 with the https:// address. Then try http://www.example.com/ and a deeper page such as http://example.com/about/. All should end on the same secure address in one hop.

If you get a redirect loop

A loop ("too many redirects") usually means Apache cannot tell the visitor is already on HTTPS. This happens when a load balancer, reverse proxy or CDN (Cloudflare for example) handles HTTPS and talks to Apache over plain HTTP. Apache then sees every request as HTTP and keeps redirecting. In that case, check the X-Forwarded-Proto header instead:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

This only works if your proxy actually sends that header. Check your proxy or host documentation. Some CDNs also have their own "always use HTTPS" setting, which can be simpler.

After the redirect works

  • Fix mixed content. Images, scripts or stylesheets still loaded from http:// addresses can trigger browser warnings. Update them to https:// in your templates, or in the database if you run a CMS.
  • Update your CMS address settings. In WordPress, the WordPress Address and Site Address (under Settings, General) should start with https://.
  • Update the sitemap, canonical tags and internal links to the HTTPS version, and add the HTTPS site in Google Search Console if you have not already.
  • Consider HSTS later. The Strict-Transport-Security header tells browsers to always use HTTPS, but it is hard to undo, so add it only once everything works and start with a short max-age.

SiteMonday checks this in week 10 (domain, SSL and email deliverability), and you can run the curl test above any Monday you like. For the general version of this problem on any platform, see the HTTP to HTTPS redirect guide.

When to ask for help

If you cannot edit virtual host files, do not know whether your host allows .htaccess rules, or a proxy is involved, your hosting support can usually tell you in a minute. A developer can also do this in a few minutes if you give them the domain and the preferred address.

Not on Apache? Read the general guide to http not redirecting to https, or the guide for WordPress, Nginx, Cloudflare, cPanel, IIS, GoDaddy.

Questions

Should I use Redirect or RewriteRule on Apache?

Use `Redirect permanent` inside the port 80 VirtualHost when you can edit server config, because it is simple and needs no extra module. Use `RewriteRule` in `.htaccess` when you only have access to your site folder.

Why does my .htaccess redirect do nothing?

Usually `AllowOverride` is set to `None` for your folder, `mod_rewrite` is not enabled, or the file is not in the document root. Ask your host, or check the virtual host settings if you manage the server.

Do I need to restart Apache after changing .htaccess?

No, Apache reads `.htaccess` on every request. Changes to virtual host files do need a reload, such as `systemctl reload apache2` or `systemctl reload httpd`.

Should I use a 301 or a 302 redirect for HTTPS?

Use a 301 (permanent) redirect, which is what `Redirect permanent` and `R=301` send. A 302 tells search engines the move is temporary.

Will Certbot set up the redirect for me?

Yes, `certbot --apache` can offer to add a redirect from HTTP to HTTPS when it installs the certificate. Check the result with `curl -I` afterwards, and review the virtual host file it changed.

More Apache fixes