Skip to content

Redirecting HTTP to HTTPS with .htaccess on a cPanel account — the per-site way

How to force HTTPS for a single cPanel account using .htaccess, the correct rule behind a proxy, and why this differs from cPanel's server-wide Force HTTPS Redirect toggle.

Once a site has a valid certificate, you want every visitor on HTTPS — not just the ones who type the https:// prefix. For a single cPanel account the cleanest tool is an .htaccess redirect that sends all HTTP requests to their HTTPS equivalent. It is per-site (so it does not touch other accounts), it lives in the account’s own document root, and the account owner can manage it without root. The one subtlety is getting the rule right when a proxy or CDN sits in front, because the naive rule can cause a redirect loop there.

What’s happening

.htaccess is Apache’s per-directory configuration file, read on every request under that directory. A redirect rule there uses mod_rewrite to detect requests arriving over HTTP and issue a 301 (permanent) redirect to the same URL on HTTPS. Because it is scoped to one account’s docroot, it is the right tool when you want to force HTTPS for this site specifically — as opposed to cPanel’s Domains » Force HTTPS Redirect toggle, which sets this up at the vhost level, or a server-wide Apache include that an administrator applies to every account at once.

The detail that trips people up is how to detect “this request is HTTP.” The straightforward %{HTTPS} off check works for a direct connection. But behind a CDN or reverse proxy (Cloudflare, a load balancer), the proxy often terminates TLS and talks to the origin over plain HTTP, so %{HTTPS} is always off at the origin even though the visitor is on HTTPS — and a rule based only on %{HTTPS} then redirects forever. In that case the rule must read the X-Forwarded-Proto header the proxy sets. Choosing the right condition for the topology is the whole job.

How to verify

First confirm the certificate is valid (no point forcing HTTPS to a broken cert) and see the current redirect behaviour:

# Cert is valid and the HTTPS site responds?
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
  | openssl x509 -noout -dates -subject
curl -sI https://example.com | head -1

# What happens to a plain HTTP request right now?
curl -sIL http://example.com | grep -iE 'HTTP/|location'

Inspect the existing .htaccess and whether a proxy is in front (which decides which rule you need):

# Current rewrite/redirect rules in the account
grep -niE 'rewrite|redirect|HTTPS|X-Forwarded' /home/<cpaneluser>/public_html/.htaccess

# Is a CDN/proxy terminating TLS? (look for proxy headers / known CDN)
curl -sI https://example.com | grep -iE 'server|cf-ray|via|x-forwarded'

After adding the rule, confirm a single clean hop and no loop:

# Should be ONE 301 to the https URL, then 200 — not a loop
curl -sIL http://example.com | grep -iE 'HTTP/|location'

A direct-connection site shows %{HTTPS} off behaving correctly; a cf-ray header (or similar) tells you TLS terminates at a proxy and you need the X-Forwarded-Proto variant.

The fix

  1. Confirm a valid certificate first — forcing HTTPS before the cert is installed and valid turns a working HTTP site into a broken HTTPS one. Verify the cert with openssl (or that AutoSSL has issued one) before adding any redirect.
  2. Add the standard direct-connection rule — for a site with no proxy in front, put this at the top of /home/<cpaneluser>/public_html/.htaccess:
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    This sends every HTTP request to the same URL on HTTPS with a permanent redirect.
  3. Use the X-Forwarded-Proto rule behind a CDN/proxy — if a proxy terminates TLS, the %{HTTPS} off rule loops. Detect the original scheme via the forwarded header instead:
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    This only redirects when the visitor’s connection was HTTP, not the proxy-to-origin hop.
  4. Place the rule above existing rewrites — put the HTTPS redirect before a CMS’s rewrite block (for WordPress, above the # BEGIN WordPress section) so the scheme is corrected before the application’s routing runs. A rule buried after other RewriteRule lines can be bypassed.
  5. Exclude the ACME challenge path — if the site uses HTTP-01 validation for AutoSSL/Let’s Encrypt, a blanket redirect can break renewals by redirecting the challenge. Add RewriteCond %{REQUEST_URI} !^/\.well-known/ before the rule so the validation token is served over HTTP.
  6. Do not stack it with another HTTPS forcer — if cPanel’s Force HTTPS Redirect toggle is on, or the CMS (a plugin like “Really Simple SSL”) is also forcing HTTPS, two redirects can conflict. Pick one owner of the redirect; if you use this .htaccess rule, turn the others off to avoid a loop.
  7. Use 301, not 302, once you are confident — a 301 is permanent and is what you want for SEO and browser caching. While testing, a 302 (temporary) avoids browsers hard-caching a redirect you might still be tuning; switch to 301 after the rule is proven.
  8. Test the redirect and then HSTS as a follow-up — confirm curl -sIL http://example.com shows one 301 to HTTPS and a 200. Once the redirect is solid across the whole site, adding an HSTS header tells browsers to skip the HTTP request entirely on future visits, which is the natural next hardening step.

For a single account this .htaccess approach gives the owner control and keeps the change scoped to their site. When the goal is forcing HTTPS across every account on the server at once, that belongs at the server/vhost level instead. Stack Harbor sets up HTTPS enforcement at whichever scope fits under cPanel server management — start a conversation to force HTTPS cleanly.