How to

How to redirect www URLs to non-www on WordPress

0
How to redirect www URLs to non-www on WordPress

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

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

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

To redirect all requests coming to www.your_URL.com to your_URL.com, you can modify the .htaccess file to add the following line at the start of IfModule mod_rewrite.c

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

Read the rest of the article to find out the details.

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

Likewise, the file is hosted on an FTP server and their address will have the term ftp in the URL structure.

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

Times change but the ‘tradition’ of placing www in the URL continues. At this time, you don’t need to enter www in the URL again. No need and make your website URL long.

All the websites I have created lately, don’t use www at the URL address. I guess you do the same thing.

The problem is that even though you know that www is of no use, visitors to your website might not know it. Some people still type manually before entering the URL. If you use browser shortcuts like Ctrl + Enter, it automatically adds www and com before and after the typed URL.

You have two options:

  • Make your website available at www and non-www URLs. But the problem is that URLs with www will be treated as subdomains and that will create problems from an SEO point of view.
  • The second option is to do 301 redirects from www to non-www URLs. In this way, even if someone types www.URL.com, they will automatically be redirected to URL.com.

Let’s see how to do it.

Redirect www URL to non-www on WordPress

1

To redirect www to a non-www domain, you must edit the .htaccess file on your server.

You can find this file in the public folder of your WordPress installation. You can use FTP tools like FileZilla to access files on your server. If possible, try using the command line for WordPress or any type of server management task. This will improve your skills.

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

In the .htaccess file, open the IfModule mod_rewrite.c section. If there isn’t, make it yourself.

Here is the code that you must 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 HTTP_HOST for https. HTTP_HOST works for http and https. There is no HTTPS_HOST.

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

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

You might have several rewrite conditions in the mod_rewrite.c section. In this case, you must redirect this www to non-www before other rules.

<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 must exist before the other code in the rewrite section. Otherwise the diversion will not function properly.

If you redirect www to non-www with https, you must ensure that your SSL certificate includes both the www and non-www versions of your URL.

If you create an SSL certificate yourself, you must create one again by entering the www subdomain into your account.

I hope this quick tip helps you drive traffic from www to non-www URLs from your WordPress site. If you have questions or suggestions, please leave a comment below.

How to Install Gradle on Ubuntu 20.04

Previous article

How to Download NVIDIA Drivers Without GeForce Experience

Next article

You may also like

More in How to