Home / Fix guides / Website not using HTTPS / Apache

Apache fix guide

How to move your website from HTTP to HTTPS on Apache

If your site loads with http:// and browsers show "Not secure", Apache is usually one certificate and one virtual host away from fixing it. This guide covers a server you control and shared hosting.

Apache can serve HTTPS very well, but it does not turn it on by itself. You need three things: the SSL module, a certificate for your domain, and a virtual host listening on port 443. Then you send visitors from the old http:// address to the new https:// one.

The steps below assume you have shell access (SSH) to your server. If you are on shared hosting without SSH, read the note in step 1 first.

What you need before you start

  • Your domain's DNS A record (and AAAA if you use IPv6) must point to this server.
  • Ports 80 and 443 must be open in your firewall and with your hosting provider.
  • You need sudo or root access, and a backup of your Apache config.

Step by step

  1. Check what kind of hosting you have. On shared hosting (cPanel, Plesk and similar) you usually cannot edit the main Apache config. Look in the control panel for "SSL/TLS", "Let's Encrypt" or "AutoSSL", turn it on for your domain, then jump to step 6. On a VPS or your own server, continue with step 2.
  1. Enable SSL support in Apache. On Debian and Ubuntu run sudo a2enmod ssl. On RHEL, Rocky, AlmaLinux and Fedora run sudo dnf install mod_ssl. The service is called apache2 on Debian and Ubuntu and httpd on the RHEL family.
  1. Get a certificate. The common free route is Let's Encrypt with Certbot and its Apache plugin. Install Certbot using the instructions on certbot.eff.org for your system, then run:
sudo certbot --apache -d example.com -d www.example.com

Certbot proves you control the domain, saves the certificate under /etc/letsencrypt/live/example.com/, and can edit your virtual host and add the redirect for you. Apache also has a built in module called mod_md that can request certificates, but Certbot is the more widely documented option. If you bought a certificate elsewhere, use the files your provider gave you instead.

  1. Check the port 443 virtual host. If you did not let Certbot write it, add one yourself. On Debian and Ubuntu these files live in /etc/apache2/sites-available/ (enable with a2ensite). On the RHEL family look in /etc/httpd/conf.d/. A minimal block looks like this:
<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>

Use fullchain.pem for the certificate file, not cert.pem, so browsers get the intermediate certificates too.

  1. Test and reload. Run sudo apachectl configtest. It should say Syntax OK. Then reload with sudo systemctl reload apache2 (or httpd). Open https://example.com in a browser and check for the padlock.
  1. Redirect HTTP to HTTPS. The cleanest way is in the port 80 virtual host:
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>

If you cannot edit the main config, put this in the .htaccess file in your site root instead:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This needs mod_rewrite enabled and AllowOverride set to allow it (at least FileInfo) for your directory. If your site sits behind a proxy, load balancer or CDN that handles HTTPS, Apache may always see plain HTTP and this rule can cause a redirect loop. In that case check the X-Forwarded-Proto header instead, or do the redirect at the proxy.

  1. Fix mixed content. After the move, pages can still load images, scripts or stylesheets from http:// addresses, and the browser will warn or block them. Open the browser developer tools (F12), look at the Console tab, and update every http:// link it lists. In WordPress, set both the WordPress Address and Site Address to https:// under Settings, General, and update old links in your content. Other CMSs have a similar base URL setting.
  1. Make sure renewal works. Let's Encrypt certificates are short lived, so renewal must be automatic. Run sudo certbot renew --dry-run to test it. If a certificate lapses, visitors see a full page warning, so see what to do when a certificate expires.
  1. Optional: add HSTS once everything works. The Strict-Transport-Security header tells browsers to use HTTPS only. It needs mod_headers and goes in the 443 virtual host. Start with a short max-age and raise it later, because browsers remember it. Details are in the missing HSTS header guide.

Common problems

  • Certbot fails to validate: DNS does not point to this server yet, or port 80 is blocked.
  • The default Apache page appears on https: your ServerName does not match, or the new site is not enabled.
  • Redirect loop: usually a proxy or CDN, or a CMS that forces its own redirect. Keep only one redirect rule.
  • Padlock with a warning: mixed content, see step 7.

Checking it from outside

Test both http://example.com and http://www.example.com and confirm each lands on your one preferred https:// address. Domain, SSL and email checks are also covered in week 10 of SiteMonday, which reports on them for your site. For the wider picture, see the general guide to sites without HTTPS.

Not on Apache? Read the general guide to website not using https, or the guide for WordPress, Cloudflare, Nginx, cPanel, GoDaddy, SiteGround.

Questions

Is HTTPS free on Apache?

Yes, Apache itself costs nothing and Let's Encrypt certificates are free. You only pay if you choose a certificate from a commercial provider.

Should I use Redirect or mod_rewrite for the HTTP to HTTPS redirect?

Use a simple `Redirect permanent` in the port 80 virtual host when you can edit the main config. Use the `.htaccess` rewrite rule only when that is not possible, such as on shared hosting.

Why does my .htaccess redirect do nothing?

Usually `AllowOverride` is set to `None` for your directory, or `mod_rewrite` is not enabled. On a VPS, fix this in the virtual host or directory block and reload Apache.

Do I need to restart Apache after installing a certificate?

A reload is enough: `sudo systemctl reload apache2` (or `httpd`). Run `apachectl configtest` first so a typo does not take the site down.

Will moving to HTTPS break my existing links?

Old http:// links keep working if you add a permanent redirect, and they land on the https:// version. You should still update internal links and your CMS address to https:// to avoid mixed content.

More Apache fixes