2

At this time I have a CNAME record with host "www" and pointing to example.com. But with this setup, when a user types www.example.com, he gets "redirected" (I know it's not an actual redirect) to example.com. I would like however to have the address stay to www.example.com when he types it.

I therefore think to delete the CNAME record and create a virtual host on Apache. What do you think would be the most correct thing I should do?

1 Answer 1

3

If the URL is changing from example.com to www.example.com for users, then there IS an actual redirect happening. However it has nothing to do you with CNAME record, because CNAMEs can't cause redirects. They simply point a domain or subdomain to the same server as another host name. So your CNAME record is causing both your bare domain and your www subdomain to resolve to the same server, but your web server is what is causing the redirect. To solve this problem, you won't need to touch your DNS records at all. Instead you'll need to modify your web server configuration.

Within Apache is is very common to point both the bare host name and the www subdomain to the same directory and configure the redirect using rewrite rules in .htaccess. If that is the case, you should delete the rewrite rules or change them to make the redirect go the other direction.

It is also possible to configure different virtual hosts for the bare domain and the www subdomain and use redirect directives in one or the other. If that is the case, you will need remove the redirect directive from one of your virtual hosts and add it to the other.

Another possibility is that you have a virtual host for www but some default catch-all virtual host for every other name that is pointing to your server that redirects. If that is the case, you should create a new virtual host for your bare domain, or add a ServerAlias to your virtual host for your www subdomain so that the same virtual host also handles the bare domain.

2
  • Thank you for the reply, everything your wrote makes sense but somehow every configuration I used wasn't working as expected. In the end I came to the conclusion that the wordpress I had installed was playing a part in this. It plays with .htaccess according to the site/wordpress url setting in the dashboard. So in the end I just wrote this in the example.conf file <VirtualHost *:80> ServerName www.example.com ServerAlias example.com DocumentRoot /var/www/example/ </VirtualHost> I then entered the dashboard and changed the aforementioned settings to www.example.com.
    – Gianf DS
    Commented Sep 14, 2022 at 15:36
  • 3
    I should have mentioned a 4th possibility that your content management system is doing its own canonicalization. Yes, WordPress can do redirects based on the WP_SITENAME and WP_HOME variables defined within it. Commented Sep 14, 2022 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.