![](/img/trans.png)
[英]Mobile redirect with .htaccess for www.example.com OR 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.com
或https://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.