Remove www From URLs and Force HTTPS With .htaccess

Quick Summary
- Five rewrite lines in your root .htaccess 301-redirect www requests to the bare domain over HTTPS and send http requests without www to HTTPS, keeping the path and query string.
- Place them above the BEGIN WordPress block and set both addresses in the General settings to https without www to avoid a redirect loop.
- If HTTPS ends at a proxy or CDN in front of Apache, use its force-HTTPS setting instead of the second rule, and Nginx needs an equivalent rule because it doesn't read .htaccess.
These Apache rewrite rules redirect www to non-www: every request for the www version of your domain goes to the bare domain over HTTPS. They also move plain http:// requests without www to HTTPS. Both use permanent (301) redirects. For example, http://www.example.com/shop/, https://www.example.com/shop/ and http://example.com/shop/ all end up at https://example.com/shop/ in a single redirect. It works on any site hosted on Apache with mod_rewrite, WordPress included.
Add the code to the .htaccess file in your site’s root folder. On a WordPress site, put it above the # BEGIN WordPress block so it runs before the WordPress rules.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,NC,L]How it works
RewriteEngine onswitches on URL rewriting.- The first
RewriteCondlets the rule below it run only when the host name starts withwww.. The(.*)part captures the rest of the host, such asexample.com, and[NC]makes the match case-insensitive. - That
RewriteRuleredirects tohttps://plus the captured host (%1) and the requested path (%{REQUEST_URI}).R=301makes the redirect permanent andLstops processing further rules for this request. A query string such as?page=2stays in place. - The second pair catches the requests that are left.
%{HTTPS}isoffwhen the connection doesn’t use SSL, so a plainhttp://example.comrequest is redirected tohttps://with the same host and path. - Together they cover all four cases: http with www and https with www go straight to
https://example.comthrough the first rule, http without www goes there through the second, and https without www isn’t redirected.
Before you use it
- If HTTPS ends at a proxy, load balancer or CDN in front of Apache, Apache receives plain http and
%{HTTPS}is alwaysoff, so the second rule would redirect in a loop. In that setup, use your host’s or CDN’s force-HTTPS setting instead of the second rule. - Your SSL certificate must cover the domain without www, and the www version too: the browser checks the certificate for
https://www.example.combefore it gets the redirect. - In WordPress, set both addresses under Settings > General to the https version without www. If they still contain www, WordPress redirects back and you end up in a redirect loop.
- Back up
.htaccessbefore you edit it. Browsers cache 301 redirects, so test in a private window. - Nginx doesn’t read
.htaccess; there you need an equivalent rule in the server configuration.
Related articles
- WordPress security rules for .htaccess: blocking xmlrpc.php, PHP in uploads and directory listings
- browser caching headers in .htaccess: Expires rules that live in the same file
