Security & SSL

How to Force HTTPS Redirect in Apache with .htaccess

Learn how to redirect all HTTP traffic to HTTPS in Apache using .htaccess and mod_rewrite, with ready-to-paste examples for any type of site.

A close-up of a chain link fence with padlocks securing it, symbolizing high security outdoors.

To force HTTPS in Apache, add these lines to your .htaccess file in the site root:

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

That's all most sites need. The rest of this article covers variants for WordPress, www domains, sites behind a reverse proxy, and common errors.

Prerequisites

Before editing .htaccess, confirm that:

  • Your site has a valid SSL certificate installed. If you don't have one yet, install Let's Encrypt via cPanel or Certbot first.
  • The mod_rewrite module is enabled in Apache (most shared hosting plans have it on by default; on a VPS, enable it with a2enmod rewrite).
  • Your Apache VirtualHost has AllowOverride All (or at minimum AllowOverride FileInfo) so Apache honors the .htaccess file.

Without a valid certificate, a permanent redirect to HTTPS will throw a browser error and block all access to your site.

The Basic Rule — Works for Almost Any Site

Open the .htaccess file in your domain root (the same folder as index.php or index.html) and add, preferably at the very top:

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

What each line does:

  • RewriteEngine On — activates Apache's URL rewriting engine.
  • RewriteCond %{HTTPS} off — the next rule only fires if the current connection is not HTTPS.
  • RewriteRule ^(.*)$ https://... — redirects the full URL (including path and query string) to its HTTPS equivalent with a 301 (permanent) status code.

Save the file, wait a few seconds, and test by visiting http://yourdomain.com. The browser should redirect you to https://yourdomain.com automatically.

Variants for Specific Cases

WordPress

WordPress generates its own block in .htaccess. Place the HTTPS rules before the WordPress block:

# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Also update WordPress itself: go to Settings → General and change both URLs from http:// to https:// so internal links and canonical tags are correct.

Force www (or non-www) at the Same Time

To redirect to HTTPS and the canonical www version simultaneously:

RewriteEngine On
# Redirect to HTTPS + www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]

For the non-www version, change the condition to RewriteCond %{HTTP_HOST} ^www\. and the target to https://yourdomain.com/$1.

Site Behind a Reverse Proxy or CDN (Cloudflare, Load Balancer)

When a proxy sits between the visitor and your server, Apache may see the connection as HTTP even though the client used HTTPS — because the proxy terminates TLS. In that case, %{HTTPS} off is always true and you'll create a redirect loop.

The fix is to check the X-Forwarded-Proto header that the proxy adds:

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

Security note: only trust X-Forwarded-Proto when you know it comes from a trusted proxy (Cloudflare or your own load balancer). An external visitor can forge this header. If you're using Cloudflare, you can also enable Always Use HTTPS directly in the Cloudflare dashboard and skip the Apache rule entirely.

Verify the Redirect Is Working

After saving .htaccess, confirm everything is correct:

  • In the browser: type http://yourdomain.com. The address bar should show https:// and the padlock.
  • With curl: run curl -I http://yourdomain.com. The response should be 301 Moved Permanently with Location: https://yourdomain.com/.
  • Online tool: use redirectcheck.com to visualize the full redirect chain and confirm there are no loops.

For more on setting up SSL correctly on your server, explore the SSL security resources — installation, renewal, and diagnostic guides all in one place.

Common Errors and How to Fix Them

Error Likely cause Fix
ERR_TOO_MANY_REDIRECTS Redirect loop (proxy + %{HTTPS} off) Use X-Forwarded-Proto if behind a proxy; verify SSL is active on the VirtualHost
403 Forbidden AllowOverride None in Apache config Change to AllowOverride All in the VirtualHost and reload Apache
500 Internal Server Error Syntax error in .htaccess Check Apache error log: tail -f /var/log/apache2/error.log
Rule has no effect mod_rewrite not enabled On VPS: sudo a2enmod rewrite && sudo systemctl reload apache2

If you need help setting up your Apache environment from scratch, the team at elenlace.com handles this as part of their managed hosting plans.

Key Takeaways

  • The basic three-line mod_rewrite rule handles HTTPS redirection for most Apache sites.
  • In WordPress, place the HTTPS rules before the block WordPress generates.
  • Behind a proxy (Cloudflare, load balancer), use X-Forwarded-Proto instead of %{HTTPS} off to avoid redirect loops.
  • Always verify with curl -I or a redirect tool that the result is a 301, not a loop.
  • Never enable the redirect without a valid SSL installed — you'll lock yourself out of your own site.

Want all of this configured correctly from day one? Sign up for hosting at elenlace.com and your site launches with HTTPS forced, SSL installed, and redirects set up out of the box.

FAQ

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

Always use 301 (permanent). This tells search engines to transfer link equity to the HTTPS URL. A 302 is temporary and Google won't consolidate SEO signals, which can hurt your rankings.

Does the HTTPS redirect slow down my site?

The impact is minimal: a 301 redirect adds one extra HTTP request only on the visitor's first access via HTTP. Modern browsers cache 301 redirects, and enabling HSTS eliminates even that first extra request.

Does this work on cPanel and shared hosting?

Yes. Most shared hosting environments with Apache have mod_rewrite active by default. In cPanel, you can also enable the HTTPS redirect from SSL/TLS → Force HTTPS Redirect, which applies the same rule automatically without touching .htaccess yourself.

What if my site is in a subdirectory?

The rules work the same way. If your site lives at yourdomain.com/store/, place the .htaccess in that subdirectory and it will only affect URLs under that path. Using %{HTTP_HOST}%{REQUEST_URI} preserves the full path automatically.

Further reading

Other providers and guides worth comparing:

← All