WordPress – How to force website to use www

The steps below detail two ways make WordPress use only www for your website address.

If a user tries to access your website using the non-www address they will be automatically directed to the www address.

Method 1: using wp-config.php

Please note, if you have the WordPress files in a sub-folder, for example domainname.com/wordpress but want users to access your site using domainname.com – you will need to set WP_HOME to the sub-folder and WP_SITEURL to the address for the website.

  1. Using FTP or your websites cPanel access, open the wp-config.php file in the root of your WordPress installation
  2. At the top of the file, below the <?php add the following code
  3. define('WP_HOME','http://www.domainname.com');
    define('WP_SITEURL','http://www.domainname.com');
  4. Replace ‘domainname.com’ with the new domain name for your website.
  5. WordPress-ChangeDomain2
  6. Save the changes and try accessing your site from the non-www address
  7. The page should automatically redirect to the www address.

Method 2: using wp-admin

Please note, if you have the WordPress files in a sub-folder, for example domainname.com/wordpress but want users to access your site using domainname.com – you will need to set WordPress Address (URL) to the sub-folder and WordPress Site (URL) to the address for the website.

  1. Log into your wp-admin
  2. Browse to ‘Settings’ -> ‘General’
  3. Under ‘WordPress Address (URL)’ and ‘Site Address (URL)’ add www to the address as shown below
  4. Save the changes and try accessing your site from the non-www address
  5. The page should automatically redirect to the www address.

WordPress-ChangeDomain1

Method 3: using .htaccess

Apache hosted websites (the most common type) can also configure a server configuration file called .htaccess

To do this you will need access to the files on the server. You should find it at the ‘root directory’ for your website (where WordPress was installed).

If you’re not sure about this – contact your website host for assistance.

Adding the following code to the top of your .htaccess will automatically redirect non-www to www (including https being enforced).

# Redirect non-www traffic to www & enforce https
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} !^static.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]