Troubleshooting

How to Redirect www URLs to non-www in WordPress

This quick tutorial demonstrates how to redirect www URLs to non-www URLs in WordPress. Both http and https versions have been discussed.

Abhishek Prakash
Abhishek Prakash

This quick tutorial demonstrates how to redirect www URLs to non-www URLs in WordPress. Both http and https versions have been discussed.

Redirect www to non-www in WordPress [Quick Summary]

To redirect all the requests coming to www.your_URL.com to your_URL.com, you can modify the .htaccess file to add the following lines in the beginning of the IfModule mod_rewrite.c

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

Read the rest of the article to know the details.

At the beginning of the World Wide Web project, WWW was used in the URL structure to denote that the URL contained set of linked hypertext documents that could be viewed on a web browser. In simpler words, a website.

Similarly, files were hosted FTP servers and their address would have the term ftp in the URL structure.

So, www.example.com means website and ftp.example.com for FTP.

Times changed but the ‘tradition’ of putting www in the URL continued. These days, you don’t need to put www in the URL anymore. It’s unnecessary and makes your website’s URL lengthy.

All the websites I have created lately, don’t use www in the URL address. I guess you did the same.

The problem is that though you know that www is pointless, your website visitors probably don’t know that. Some people still manually type www before the entering the URL. If you use browser shortcut like Ctrl+Enter, it automatically adds www and com before and after the typed URL.

You have two options:

  • Make your website available on both www and non-www URL. But the problem is that the URL with www will be treated as a subdomain and that will create an issue from SEO point of view.
  • The second option is to do a 301 redirect of www to non-www URL. This way, even if someone typed www.URL.com, he/she will be automatically redirected to URL.com.

Let’s see how to do that.

Redirect www URLS to non-www in WordPress

Redirect Www To Non Www

To redirect the www to non-www domain, you’ll have to edit the .htaccess file on your server.

You can find this file in the public folder of your WordPress install. You can use a FTP tool like FileZilla to access the files on your server. If possible, try to use command line for WordPress or any kind of server management tasks. It will improve your skills.

It’s up to you how you edit the file.

In the .htaccess file, go to the IfModule mod_rewrite.c section. If it doesn’t exist, create it yourself.

Here’s the code that you should add at the beginning of this section:

RewriteCond %{HTTP_HOST} =www.your_URL.com RewriteRule (.*) https://your_URL.com/$1 [R=301,L]

Don’t confuse with HTTP_HOST for https. HTTP_HOST works for both http and https. There is no HTTPS_HOST.

If your website doesn’t use SSL, you can use http instead of https in the URL of your website.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} =www.your_URL.com
RewriteRule (.*) https://your_URL.com/$1 [R=301,L]
</IfModule>

You may have some rewrite conditions in the mod_rewrite.c section. In that case, you must put this www to non-www redirect before any other rule.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} =www.your_URL.com
RewriteRule (.*) https://your_URL.com/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Attention

The redirect code should go before any other code in the rewrite section. Otherwise the redirection won’t work properly.

If you are redirecting www to non-www with https, you should make sure that your SSL certificate covers both www and non-www versions of your URL.

If you generated SSL certificate yourself, you should regenerate it taking www subdomain into account.

I hope this quick tip helped you in redirecting the traffic of www to non-www URL your WordPress website. If you have any questions or suggestions, feel free to leave a comment below.



Join the conversation.