为什么我的 URL 重定向在没有 www 的情况下不起作用?

use*_*461 5 https redirect cname namecheap

我想要以下重定向:

http://example.com -> http://www.example.com
example.com -> http://www.example.com
www.example.com -> http://www.example.com
Run Code Online (Sandbox Code Playgroud)

在我的 namecheap 中,我设置了两个 URL 重定向记录:

CNAME Record        | www | appname.us-east-2.elasticbeanstalk.com.
URL Redirect Record |  @  | http://www.example.com                  | Unmasked 
Run Code Online (Sandbox Code Playgroud)

但是,当我实际输入这个完整的 URL 时,我仍然只能访问http://www.example.com 。当我删除 www 时,请求超时。所以 URL 重定向似乎没有做任何事情。如何正确重定向到我的完整 http 域?

fla*_*sen 3

我使用 NGINX 并实现了如下重定向:

vi /etc/nginx/sites-enabled/example-com

server {
        listen 80;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
}

server {
        listen 80;
        server_name www.example.com;
        root /var/www/html;
        index index.html index.htm;
}
Run Code Online (Sandbox Code Playgroud)

响应状态代码 301 表示“永久移动”,用于永久 URL 重定向。

请不要忘记重新启动 NGINX。

sudo systemctl restart nginx
Run Code Online (Sandbox Code Playgroud)