An .htaccess HTTPS redirect can work behind Cloudflare or another TLS proxy, but the generated rule must not be deployed unchanged when Apache receives plain HTTP from the proxy.

The deciding factor is where TLS terminates and what Apache sees on the proxy-to-origin connection. An HTTPS request at the proxy does not automatically mean Apache receives HTTPS. If the origin connection is also encrypted, Apache can evaluate the request normally. If the proxy sends plain HTTP to Apache, the redirect must use a different, trusted way to identify the original request scheme.

can i use htaccess https redirect behind cloudflare or another tls proxy
Can I Use .htaccess HTTPS Redirect Behind Cloudflare?

How TLS termination changes the redirect result

There are two relevant hops in a proxied request: the browser-to-proxy connection and the proxy-to-Apache connection. The generated redirect checks Apache's own HTTPS state as well as the Host header. Its fixed-host behavior can still enforce the canonical hostname when the proxy forwards the expected host, but its HTTPS condition is the part most likely to fail behind a TLS-terminating proxy.

Request path What Apache sees Implication for the generated rule
TLS ends at Apache An HTTPS request The generated HTTPS condition is appropriate, subject to normal Apache and staging checks.
TLS ends at the proxy, which also uses TLS to Apache An HTTPS request Apache can use its normal HTTPS state; the fixed-host check remains relevant.
TLS ends at the proxy, which sends HTTP to Apache A plain HTTP request The unchanged rule can redirect repeatedly because Apache believes every origin request is insecure.
TLS ends at the proxy and a trusted forwarded-protocol header is used Plain HTTP plus a trusted protocol header A manually adapted rule can work, but the generator does not create that proxy-aware condition.

Apache documentation explains the rewrite processing involved in mod_rewrite rules. The important point for proxy deployments is that the condition must reflect the request's client-facing scheme, not simply whether the proxy used HTTP when talking to the origin.

Why an unchanged rule can loop

A typical proxy redirect loop follows a short sequence:

  • The browser requests the canonical HTTPS URL.
  • The proxy accepts the HTTPS connection but forwards the request to Apache over plain HTTP.
  • Apache sees a non-HTTPS request and returns a 301 redirect to the same fixed HTTPS URL.
  • The browser follows the redirect, and the proxy again sends an HTTP request to Apache.

The canonical host does not prevent this loop. A fixed destination is still safer than building an external URL from an untrusted Host value, but it solves hostname control rather than TLS-state detection. The proxy must also forward the host you expect; otherwise the fixed-host check can introduce a separate redirect decision.

Generate a narrow fixed-host baseline

Use the Htaccess Generator to create a deliberately narrow Apache 2.4 baseline for fixed-host HTTPS canonicalization, optional directory-listing disablement, and an optional local custom 404 path. It is not a proxy detector, server inspector, or complete configuration builder.

Enter the exact canonical origin, such as https://www.example.com. The input must contain only a public domain origin: no credentials, port, path, query, or fragment. IP addresses and non-origin URLs are rejected. The generated redirect always targets HTTPS, escapes hostname dots in the regular expression, enables mod_rewrite, and checks whether Apache sees a non-HTTPS request or a Host different from the fixed canonical domain. It then issues one 301 redirect to that fixed host while preserving REQUEST_URI.

Generated component Purpose Required verification
Fixed-host HTTPS redirect Redirects non-HTTPS or alternate-host requests to one HTTPS hostname while preserving the path. Confirm mod_rewrite is available, place the file in the document root, and test the real request path.
Optional Options -Indexes directive Asks Apache not to generate a directory listing when no index resource is present. AllowOverride must permit Options; otherwise Apache may return a 500 error.
Optional local ErrorDocument Sends missing-page responses to one local path beginning with a slash. AllowOverride must permit FileInfo, and the target must exist without causing an error loop.

Select only the optional directives your host permits. The local error input rejects external URLs, whitespace, queries, and fragments. Apache's custom error response documentation explains the behavior of ErrorDocument. The generator deliberately does not synthesize an entire server configuration, so existing CMS, authentication, cache, compression, and access-control directives still require careful review.

Deploy the redirect behind the proxy

  1. Map the request path. Record whether the browser-to-proxy connection uses TLS, whether the proxy-to-Apache connection uses TLS, and what Host value Apache receives. Do not assume an HTTPS URL at the proxy means the origin request is also HTTPS.
  2. Choose where the redirect should live. Prefer the proxy or an Apache virtual host if that layer already owns canonical redirects. Otherwise, use end-to-end TLS to Apache or deliberately adapt the Apache condition to a trusted forwarded-protocol header. Avoid combining proxy, virtual-host, and .htaccess redirects without mapping their order.
  3. Generate the fixed-host baseline. Enter the final canonical HTTPS origin in the Htaccess Generator and select only permitted options. Remember that the output contains no setting for a proxy header. If the origin receives plain HTTP, any proxy-aware condition must be designed and reviewed separately.
  4. Back up and merge the .htaccess file. Preserve the current file before making changes. Review CMS front-controller rules, authentication requirements, cache headers, compression settings, access restrictions, and directive order. Replacing the entire file can interrupt routing or bypass controls the application depends on.
  5. Deploy to staging and validate Apache. Use the host's supported configuration check, such as apachectl -t or an equivalent facility, and inspect the error log for missing modules or disallowed directives. The generator itself cannot detect Apache, inspect AllowOverride, validate certificate coverage, or run a server-side configuration test.
  6. Exercise representative requests. Test HTTP, HTTPS, the canonical host, an alternate host, a missing page, and URLs containing both paths and queries. Confirm that the redirect reaches the canonical HTTPS site without looping, that REQUEST_URI survives, and that any selected 404 target returns a real page. Restore the backup immediately if requests loop or return a 500 error.

Trust forwarded protocol data carefully

When a proxy terminates TLS, a proxy-aware Apache rule may inspect a forwarded-protocol header such as X-Forwarded-Proto. That is safe only when the proxy reliably overwrites the header, untrusted clients cannot inject a conflicting value, and the header describes the client-facing scheme. If it merely reports that the proxy-to-origin hop used HTTP, it will never indicate that the browser used HTTPS.

Control access to the origin as part of that trust model. Otherwise, a direct request could carry a forged header and produce an inconsistent redirect decision. Keep the final destination fixed to the canonical hostname even after adapting the condition. The Htaccess Generator's fixed-target approach avoids constructing an external redirect from an untrusted Host value, and that protection should remain in place.

Verify the complete path before production

Test behavior rather than merely confirming that the file was uploaded. An HTTP request should reach the intended canonical HTTPS path, an alternate host should be redirected to the fixed host, and query strings should remain intact. A missing-page request should either use the selected local ErrorDocument or follow the host's normal error behavior. Also verify that directory-listing behavior matches the option you enabled.

Permanent 301 responses can remain cached, so a staging mistake may continue affecting visitors after the server configuration is corrected. Review the final configuration and logs, deploy during a reversible window, and retest after any proxy setting, certificate, or routing change. If you control the main Apache configuration, a virtual-host Redirect directive may be simpler than per-directory rewrite logic, whose behavior differs from virtual-host configuration.

If you're weighing options, How to Check htaccess to Nginx Conversion Results covers this in detail.

If you're weighing options, Does htaccess HTTPS Redirect Replace CMS htaccess? covers this in detail.