DNS problem with www version of the website not loading when non www version of the website works

The non www version of the website works but the www version of the website does not work. Flushing cache has not resolved the problem. I have added this lines to the .htaccess to switch the www version of the website to the non www version of the website but this does not work.

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

I am using the same domain name for hosting cyberpanel (on a google cloud vm instance with static ip) as being used for the website created within cyberpanel. The nameservers for the domain are pointed to google cloud and the dns records in google cloud dns have an A record and CNAME record set up correctly. The website created within cyberpanel also has dns records with the A record and CNAME record set up correctly to match dns records in google cloud dns.

The website loads fine without using www but continues not load when using www. Please advise how to resolve this problem.

what is the error that you are getting at www version?

The error when using the www version of the website is “This site can’t be reached” error. It is strange that www version of the website will not accept a 301 redirect in .htaccess however the non www version of the website will accept a 301 redirect in .htaccess. There must be something overriding .htaccess for the www version of the website.

I get a response when I ping the non www version of the website but no response for the www version of the website.

Ping is now working for both the www and non-www version of the website however now getting a 404 Page not found error.

dns for www are set correctly?

I am still trying to resolve the problem that my website is giving a 404 not found error. The is my setup:

  1. I have the nameservers for my domain pointing to google cloud.
  2. I have a google cloud VM Instance using a fixed IP.
  3. In Google Cloud DNS I have an A record for the IP and www CNAME record.
  4. Cyberpanel is installed using SSH associated with the VM instance.
  5. In Cyberpanel I have listed the website using the same domain name.
  6. The DNS records in Cyberpanel for the domain have replicated A record and Cname record to match records in Google Cloud DNS.
  7. The nameserver in Cyberpanel is also pointing to google cloud.

What Have I done wrong what have I missed?

Following up on this topic. The issue was resolved by deleting the website within Cyberpanel and reinstalling it. Unfortunately a lot of changes to the website were lost as I was not able to access the site to save the latest version. I assume the problem occurred because the VM instance in Google Cloud had Wordpress installed and was running the website prior to the install of Cyberpanel. Wordpress needed to be activated within Cyberpanel in this scenario.