[英]How to redirect www.example.com to example.com in NginX?
When we open example.com
(without HTTPS), the site loaded very well but when we open www.example.com
(without HTTPS) it opens "Welcome of nginX" but I do not need this, we want to open example.com
even if the URL is www.example.com
. 当我们打开
example.com
(不使用HTTPS)时,该网站加载得很好,但是当我们打开www.example.com
(不使用HTTPS)时,它会打开“ Welcome of nginX”,但是我不需要这个,我们想打开example.com
即使该网址是www.example.com
。
NOTE: our site has "HTTPS" and when we open example.com
, it will be redirected to https://example.com
. 注意:我们的网站带有“ HTTPS”,当我们打开
example.com
,它将被重定向到https://example.com
。
NOTE: when we open https://www.example.com
, the site loaded well but when the URL is www.example.com
or https://www.example.com
it was not redirected. 注意:当我们打开
https://www.example.com
,该网站加载良好,但是当URL为www.example.com
或https://www.example.com
时,未将其重定向。
We are using Ubuntu at AWS. 我们在AWS上使用Ubuntu。
To do this I would recommend editing your nginx configuration file (usually in /etc/nginx/sites-enabled/you-example-config), adding return 301 as seen here , something like: 要做到这一点,我会建议编辑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...]
}
This will cause all requests to return https://example.com 这将导致所有请求都返回https://example.com
Have you defined a separate server for each of the options? 您是否为每个选项定义了单独的服务器? See example snippet below.
请参见下面的示例代码段。 Your question looks very similar to this question and this question which both have a lot more information that I won't copy paste here.
您的问题看起来与此问题非常相似, 这两个问题都有很多信息,我将不在此处复制粘贴。
This link might also help creating-nginx-rewrite-rules 此链接也可能有助于创建-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
is the protocol (HTTP or HTTPS) and $request_uri
is the full URI including arguments. $scheme
是协议(HTTP或HTTPS), $request_uri
是包含参数的完整URI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.