WordPress issue with the URL when we change non-WWW to WWW
Today I will review an issue which I have encountered many times during my work with WordPress. The issue concerns the case when we install WordPress for first time and we want to change the non-WWW URL to WWW URL. For example:
There are cases when after this change, you will no longer be able to access your website through example.com or www.examle.com. If you have that issue and you don’t know how to fix it, here are the things which you must consider:
- The web service (Apache, NGINX or else) must be configured correctly.
- The access rights to the directories and the files corresponding to the WordPress installation must be the right ones because when the web service tries to access a file or to update something, it must be able to do it.
- WordPress must be with the right configurations.
If all requirements above are fulfilled, we must check one more thing – the DNS records of the domain name.
Every time when we hit example.com, the request will be sent to the nearest DNS server which will find out that the request must be forwarded to the IP address of the server where we host the website. Let’s mark that IP with IP_ADDRESS_OF_THE_HOST_MACHINE. But there might be cases when your DNS record miss something very important, they miss the configuration for the situation when somebody requests the domain name with WWW – www.example.com. From DNS point of view, this is a different domain name and if there is no record for it, the DNS won’t know where to forward the request. This will lead us to the followins scenario:
- We log in to the WordPress administration.
- The DNS server works because the requests to example.com are redirected to our hosting machine. That’s why we can log in to the administration, we can edit the content of the website, etc.
- The next step is to change the domain name from non-WWW to WWW and save.
- That’s the moment when we will encounter an issue because the DNS server doesn’t have a record for the domain name www.example.com which contains WWW. And also if we try to access the website through example.com, WordPress (because it was said so) will redirect us to www.example.com. This is a closed circle and there is no escape from it.
The solution is very simple actually (which I hope will work and for you). Just go to the administration of your domain name and add the following CNAME record:
Hostname: * Target Host: example.com
This CNAME record is called a Wildcard record because what it does is to tell the DNS servers to forward all requests to www.example.com to the IP address associated with the domain name example.com.
That issue is not a huge obstacle as you can see, but sometimes you can struggle with it. So bear in mind all things above when you work with WordPress.
That’s from me for now. As always, any feedback in the comments is welcome!