Overview
This article explains how to force or remove the 'www' subdomain from your website URL using an .htaccess file.
Background
By default, your fully hosted website can be visited with or without the 'www' subdomain in front of it, For example:
- example.com
- www.example.com
Some people prefer to remove the 'www' subdomain in front of their website URL, while others prefer that it remains there. This article explains how to force one option or the other.
Adding or removing 'www' using an .htaccess file
You can add or remove 'www' using an .htaccess file. This involves creating and editing this file on your web server. See the following articles for information on creating or editing this file:
Make sure this file is located in the website directory.
Remove 'www'
Add the following lines to automatically remove the 'www' from the beginning of your domain.
RewriteEngine On RewriteCond %{HTTP_HOST} ^www.example.com RewriteRule (.*) https://example.com/$1 [R=301,L]
Force 'www'
Add the following lines to force the 'www' at the beginning of your domain.
RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Adding or removing 'www' in WordPress
WordPress sites can modify how the website URL appears within the WordPress dashboard. See this article for more information on how to modify a WordPress site URL.
When adding or removing 'www' breaks WordPress
WordPress is unique in that it requires the URL to either force or remove 'www' from the domain name. If the database is forcing one option, but the .htaccess file is forcing another, you'll see a redirect loop error on your website.
To fix this error:
- Make sure any customizations to your .htaccess file match what you selected within the WordPress dashboard.
- Update all URLs in the WordPress database to match your .htaccess configuration.