.htaccess Force HTTPS & WWW Redirect Generator

Updated October 2026

Generate the .htaccess block that sends http, www and non-www variants to your one canonical address with a single 301, with a proxy-safe option for Cloudflare and load balancers, optional HSTS, and a test of all four URL variants. Free, runs in your browser.

Advertisement

Advertisement

1. What should every visitor end up on?

Host name

2. Server details

Redirect status

Your .htaccess block

RewriteEngine On
# Canonical host / HTTPS redirect: keep this block above any CMS rules (e.g. "# BEGIN WordPress")
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

Check before you deploy

  • If TLS terminates at Cloudflare or a load balancer, %{HTTPS} is "off" even for secure requests and this rule loops. Tick "behind a proxy" in that case.

What it does, line by line

  1. Condition 1: the request is not HTTPS, OR
  2. Condition 2: the host does not start with "www."
  3. Last condition: capture the host without any leading "www." as %1 (always true; it only exists to capture).
  4. Rule: one 301 redirect to https://www.<host> plus the original path and query. Scheme and host are fixed in a single hop, so visitors never see two redirects.

Proof: one hop for every variant

The block above, simulated against the four ways a visitor can type your address.

  • http://example.com/page?x=1 → 301 → https://www.example.com/page?x=1
  • http://www.example.com/page?x=1 → 301 → https://www.example.com/page?x=1
  • https://example.com/page?x=1 → 301 → https://www.example.com/page?x=1
  • https://www.example.com/page?x=1 served as is (already canonical)

Why Force HTTPS and One Host Name in .htaccess

Every site is reachable in at least four ways: http://example.com, http://www.example.com, https://example.com and https://www.example.com. If all four answer with the page instead of redirecting to one of them, search engines see four copies of every URL, links and rankings get split between them, and browsers show "not secure" on the http versions. The fix is a single redirect block at the top of your .htaccessthat sends the three wrong variants to the one canonical address with a 301, in one hop. The generator above writes that block for the combination you choose.

The Standard .htaccess Force HTTPS Rule

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

%{HTTPS} is "on" for a TLS connection and "off" otherwise. The empty pattern ^ matches every path; the substitution rebuilds the same URL with https://, the original host and the original path (%{REQUEST_URI} keeps the leading slash). The query string is carried over automatically. R=301 makes it permanent and L stops later rules from touching the target.

Behind Cloudflare or a load balancer

When TLS terminates at a proxy, Apache itself receives plain HTTP, so %{HTTPS} is always "off" and the rule above redirects forever ("too many redirects"). Test the header the proxy adds instead:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Tick "behind a proxy" in the generator to get this variant. On Cloudflare also set SSL/TLS mode to "Full"; in "Flexible" mode the origin never sees HTTPS and the loop comes back.

www to non-www (and non-www to www) in One Hop

Doing the scheme and the host in two separate rules costs visitors two redirects (http://example.com → https://example.com → https://www.example.com). The block below handles both at once by using [OR] between the two conditions and capturing the host without any www. prefix as %1:

# Force HTTPS + www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

# Force HTTPS + non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

The third condition always matches; it exists only to capture the bare host. The "proof" panel in the generator runs the finished block against all four variants so you can see that each wrong one gets exactly one 301 and the right one is served untouched.

Which Host Should Be Canonical: www or non-www?

For search engines it does not matter; what matters is picking one and redirecting the other everywhere (server, sitemap, canonical tags, internal links). Practical differences:

www.example.comexample.com
CookiesCookies set on www do not leak to other subdomains (api., cdn.)Cookies on the bare domain are sent to every subdomain
DNSCan be a CNAME, which some CDNs and hosts requireThe apex cannot be a CNAME; needs ALIAS/ANAME or an A record
AppearanceLooks traditionalShorter

If you already rank with one of them, keep it. Changing the canonical host is a site move and costs a few weeks of volatility even when done perfectly.

HSTS: Making the HTTPS Redirect Permanent in the Browser

A 301 still lets the first request of every visit travel over plain HTTP. The Strict-Transport-Security header tells browsers to rewrite http:// to https:// themselves for the next year, before any request is sent:

<IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>

Add it only once the redirect works on every subdomain, because browsers that received the header will refuse to load anything over http on this domain. The preload directive goes one step further and gets the domain hard-coded into Chrome, Firefox and Safari; it is effectively permanent, so leave it off unless you intend to submit the domain to hstspreload.org.

Common Mistakes and Redirect Loops

  • Block placed below the WordPress rules. WordPress ends its block with a catch-all and [L]; anything after it never runs. Put this block above # BEGIN WordPress.
  • Site URL still set to http in the CMS. The server redirects to https, WordPress redirects back to the http "Site Address". Update the setting first.
  • Proxy without X-Forwarded-Proto. See above; use the proxy variant or fix the proxy.
  • Mixed content. Forcing HTTPS does not fix images and scripts still linked as http://. Search-and-replace them, or add Content-Security-Policy: upgrade-insecure-requests.
  • Forgetting the certificate on the redirected host. The redirect from https://example.com to www needs a certificate valid for example.com; otherwise visitors see a warning before the redirect.
  • Testing in a browser that cached a wrong 301. Use a private window or the broken link checker, which shows the uncached first response.

Nginx and Other Servers

Nginx has no .htaccess; the same redirects are separate server blocks with return 301. The Nginx rewrite generator writes them. For single-URL moves on Apache use the .htaccess 301 redirect generator, and for regex rules with other conditions the RewriteRule generator. You can paste any finished block into the .htaccess tester to walk through it line by line.

How to Use

  1. 1

    Tick "Force HTTPS" and choose the host policy: force www, force non-www, one exact host, or keep the host

  2. 2

    If TLS ends at Cloudflare, a load balancer or a hosting proxy, tick "behind a proxy" so the rule tests X-Forwarded-Proto

  3. 3

    Pick the status code (301 for a permanent canonical host) and optionally add the HSTS header

  4. 4

    Check the proof panel: all four ways to type your address should end on the canonical one with a single redirect

  5. 5

    Copy the block to the top of your .htaccess, above any WordPress or framework rules

Why This Tool Matters for SEO

http, https, www and non-www are four addresses for the same page. Without one redirect block, search engines index duplicates and split link equity between them, browsers flag the http copies as not secure, and visitors who type the wrong one bounce through two redirects. A correct block fixes scheme and host in one 301; a wrong one loops behind a proxy or sends everyone through a chain. This generator writes the single-hop version and proves it on your domain.

Frequently Asked Questions

How do I force HTTPS with .htaccess?

Add RewriteEngine On, then RewriteCond %{HTTPS} off and RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] at the top of the file. The generator writes exactly this, or the X-Forwarded-Proto variant when your site is behind a proxy.

Why does my HTTPS redirect loop ("too many redirects")?

Almost always because TLS terminates at a proxy or CDN (Cloudflare in Flexible mode, a load balancer, a hosting panel). Apache then receives plain HTTP for every request, %{HTTPS} is always "off", and the rule redirects forever. Test %{HTTP:X-Forwarded-Proto} !https instead, and on Cloudflare set SSL mode to Full.

How do I redirect www to non-www (or non-www to www) in .htaccess?

Use a RewriteCond on %{HTTP_HOST}: ^www\. [NC] to catch www requests (or !^www\. [NC] for the reverse), capture the bare host with ^(?:www\.)?(.+)$ and redirect to https://%1%{REQUEST_URI} (or https://www.%1...). Combined with the HTTPS condition and [OR], scheme and host are fixed in one hop; the generator writes the combined block.

Should the block go before or after the WordPress rules?

Before. The WordPress block ends with a catch-all rule and the L flag, so anything placed after it never runs. Put the redirect block above "# BEGIN WordPress" and also set the WordPress Site Address to the https canonical host, or WordPress will redirect back.

Is 301 or 302 right for HTTPS and www redirects?

301. The move is permanent and you want search engines to consolidate signals on the canonical URL. 308 is equivalent for GET and additionally preserves POST; 302 keeps the old URLs indexed and should only be used for a temporary test.

Do I need HSTS as well?

It is a security improvement, not an SEO one. With HSTS, browsers that have visited once go straight to https:// without an http request, which removes the redirect for returning visitors and prevents downgrade attacks. Enable it only after HTTPS works on every subdomain, and leave "preload" off unless you intend to submit the domain to the preload list.

Related Tools You Might Like

More in Technical SEO tools · All free SEO tools

Local Processing: Your data never leaves your browser