Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DNS Records Redirect www to non-www

Tags:

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;     ... 
like image 693
Matt McManis Avatar asked Mar 28 '17 23:03

Matt McManis


People also ask

How do I redirect www to non www using CNAME?

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.

How do I forward a domain without www?

Select your domain name from the drop down menu on the next line. In the redirects to text box, type in the full URL of your domain, including www (e.g. http://www.yourdomain.com). Select the radio button next to Do Not Redirect www.


2 Answers

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.

like image 75
Richard Smith Avatar answered Oct 05 '22 12:10

Richard Smith


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 
like image 36
ffmaer Avatar answered Oct 05 '22 14:10

ffmaer