繁体   English   中英

如何在NginX中将www.example.com重定向到example.com?

[英]How to redirect www.example.com to example.com in NginX?

当我们打开example.com (不使用HTTPS)时,该网站加载得很好,但是当我们打开www.example.com (不使用HTTPS)时,它会打开“ Welcome of nginX”,但是我不需要这个,我们想打开example.com即使该网址是www.example.com

注意:我们的网站带有“ HTTPS”,当我们打开example.com ,它将被重定向到https://example.com

注意:当我们打开https://www.example.com ,该网站加载良好,但是当URL为www.example.comhttps://www.example.com时,未将其重定向。

我们在AWS上使用Ubuntu。

要做到这一点,我会建议编辑nginx的配置文件(在一般情况在/ etc / nginx的/ /你-例如,配置启用的站点- ),增加收益301所看到这里 ,是这样的:

server {
    listen 80;
    listen 443;
    server_name www.example.com example.com;
    return 301 https://example.com$request_uri;
    #[...ssl settings go here, certs etc....]
}
server {
    listen 443;
    server_name example.com;
    # [...ssl and main settings go here...]
}

这将导致所有请求都返回https://example.com

您是否为每个选项定义了单独的服务器? 请参见下面的示例代码段。 您的问题看起来与此问题非常相似, 两个问题都有很多信息,我将不在此处复制粘贴。

此链接也可能有助于创建-nginx-rewrite-rules

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

server {
    listen 80;
    server_name example.com;
    # here goes the rest of your config file
    # example 
    location / {

        rewrite ^/cp/login?$ /cp/login.php last;
        # etc etc...

    }

}

$scheme是协议(HTTP或HTTPS), $request_uri是包含参数的完整URI。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM