Guides › HTML / PHP sites › Domain, DNS & SSL

Redirect HTTP to HTTPS and www to non-www With .htaccess

Visitors reach your site at several addresses. Redirect everything to one address with a few .htaccess rules, without causing a loop.

Reference guide. This guide is written from established practice and the official documentation of the tools involved. We have not yet tested every step on a live demo site. Back up your site before you change anything.

Your site can be reached as http:// or https://, with or without www. For visitors and search engines you want just one version. This works on Apache servers (including LiteSpeed), through the .htaccess file in your site root. Take a backup before editing.

Force https

Add this above other rules:

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

Redirect www to non-www

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]

Redirect non-www to www

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

Use one of the last two, not both.

Test it

Open all four versions of your address and check that each lands on your chosen one.

Avoid a loop

A loop shows "too many redirects". Do not add the same rule twice, and do not use these rules if your CDN already redirects. If your CDN is in Flexible SSL mode, switch to Full. See our redirect guide.

Permanent redirects are cached

Browsers remember 301 redirects. While testing, use a private window, or use R=302 and change it to 301 when it works.

Nginx servers

Nginx does not read .htaccess. Ask your host or edit the server block.

Related guides

Did this solve it? If not, email hello@sitefixlab.org with the exact error text and we will look into it. Try the problem finder.

Last updated 2026-11-21.