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.
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
- How to Move a WordPress Site to a New Host or Domain: Checklist
- How to Create a Subdomain (blog.yourdomain.com, shop.yourdomain.com) and Point It Somewhere
- "Not Secure" Warning or Padlock Missing After Installing SSL (Mixed Content)
- Free SSL (Let's Encrypt) vs Paid Certificate: Which Do You Need?