21
votes

I'm using Namecheap Domains and Vultr Hosting.

I'm trying to redirect DNS www to non-www.

www.example.com to example.com


I contacted Vultr and asked how to do this with their DNS Manager, they said they would not help as it is self-managed. So I contacted Namecheap, they said they would not help becuase they don't have access to Vultr's DNS Manager, would not tell me if the records I showed them are correct, and I would need to contact Vultr. So I am in an endless support loop.


Vultr DNS Manager

I followed this answer on how to set up a CNAME to redirect to non-www.

Type   | Name | Data         | Seconds
--------------------------------------
A      |      | ipv4 address | 300
AAAA   |      | ipv6 address | 300
CNAME  | .    | example.com  | 300
CNAME  | www  | example.com  | 300

After waiting all night for it to propgate, the www can still be visited and does not redirect.

It does not allow me to make another A record, only CNAME. It says:

Unable to add record: A CNAME record is not allowed to coexist with any other data. 

NGINX

I followed this guide and tried redirecting it with sites-available config. Http and Https work, but www does not redirect to non-www.

server {
    # Redirect http to https
    listen 80;
    return 301 https://$host$request_uri;
}

server {
    # Redirect www to non-www
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 443 ssl default_server;

    ssl on;
    ssl_certificate /etc/nginx/ssl/cert_chain.crt;
    ssl_certificate_key /etc/nginx/ssl/example_com.key;
    ssl_protocols  TLSv1.1 TLSv1.2;

    server_name example.com;
    ...
2
What is "www.example.com" serving for you? - Steve Harris
@SteveHarris It serves the website as normal, the same as non-www. - Matt McManis
What happens if you remove the "." (dot) entry from your DNS manager? - Allen Luce
@AllenLuce It was put there automatically by Vultr when I added the domain. If I edit it, it turns into a star *. I'm not sure I should remove it. - Matt McManis
A dot typically means "this domain." If that's true for you, it means you've got a CNAME on the domain level (i.e. a CNAME for example.com). Since you've already got A/AAAA records on the domain, this leads to an invalid configuration. What do you get when you dig example.com any (but with your domain)? - Allen Luce

2 Answers

26
votes

DNS cannot redirect your www site to non-www. The only purpose of DNS is to point both www and non-www to your server's IP address using A, AAAA or CNAME records (it makes little difference). The nginx configuration is responsible for performing the redirect from www to non-www.

Your second server block is intended to redirect from www to non-www, but currently only handles http connections (on port 80).

You can move the default server and use that to redirect everything to the intended domain name. For example:

ssl_certificate /etc/nginx/ssl/cert_chain.crt;
ssl_certificate_key /etc/nginx/ssl/example_com.key;

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;
    ...
}

Assuming that you have a common certificate for both the www and non-www domain names, you can move the ssl_ directives into the outer block and allow them to be inherited into both server blocks (as shown above).

See this document for more.

4
votes

You can direct www to non-www by adding a new server block to your nginx configuration file.

Step 1: Add the following server block to your nginx configuration file.

server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

Step 2: Restart nginx.

sudo systemctl restart nginx