DNS记录将www重定向到非www [英] DNS Records Redirect www to non-www

查看:329
本文介绍了DNS记录将www重定向到非www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Namecheap 域名和 Vultr 托管。



我正在尝试将DNS www重定向到非www。



www.example.com to example.com






我联系了Vultr,并问道如何使用他们的DNS管理员,他们表示不会帮助,因为它是自我管理的。所以我联系了Namecheap,他们表示不会因为他们无法访问Vultr的DNS管理员而不能帮助,不能告诉我我的显示记录是否正确,我需要联系Vultr。所以我在无休止的支持循环中。






Vultr DNS管理器



我遵循此答案,了解如何设置CNAME重定向到非www。

  Type |名称|数据|秒
--------------------------------------
A | | ipv4 address | 300
AAAA | | ipv6 address | 300
CNAME | 。 | example.com | 300
CNAME | www | example.com | 300

等待一夜之后,它仍然可以访问,不会重定向。



它不允许我再创建一个 A 记录,只有 CNAME 。它说:

 无法添加记录:CNAME记录不允许与任何其他数据共存。 






NGINX p>

我遵循本指南,并尝试使用sites-available配置重定向它。 Http和Https工作,但www不重定向到非www。

 服务器{
#将http重定向到https
听80;
return 301 https:// $ host $ request_uri;
}

服务器{
#将www重定向到非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;
...


解决方案

DNS无法重定向www网站到非www。 DNS的唯一目的是使用 A AAAA 或将www和非www指向服务器的IP地址 CNAME 记录(几乎没有区别)。 nginx 配置负责执行从www到非www的重定向。



您的第二个服务器块是从www重定向到非www,但目前只处理 http 连接(在端口80)。



你可以移动默认服务器并使用它来将所有内容重定向到预期的域名。例如:

  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;
...
}

假设您有两个共同的证书www和非www域名,您可以将 ssl _ 指令移动到外部块,并允许它们继承到两个服务器块(如上所示)。 p>

请参阅此文档更多。


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;
    ...

解决方案

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.

这篇关于DNS记录将www重定向到非www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆