我看過這個問題,該問題描述了如何在nginx中重定向網址,如下所示
# main server block for www.test.com server { listen 80; server_name www.test.com; ... } # redirect test.com to www.test.com server { server_name test.com; return 301 $scheme://www.test.com$request_uri; }
我需要做的是重定向一組單獨的頁面,所以想知道如何做到這一點
例如test.com\index
,test.com\home
,test.com\main
來test.com\index.php
然后,我還有其他頁面可以簡單地重定向到.php擴展名
例如test.com\about
到\test.com\about.php
做這個的最好方式是什么?
找到了答案…假設以下服務器塊 test.com
server { listen 80; server_name www.test.com; ... }
將適當的正則表達式位置路徑添加到重定向URL ,rewrite
或return
將其添加到重定向URL。
為test.com\index
,test.com\home
,test.com\main
以test.com\index.php
location ~ ^/(index|home|main) { rewrite ^/.* http://$server_name/index.php permanent; }
對於test.com\about
以\test.com\about.php
location /about { rewrite ^/.* http://$server_name/about.php permanent; }
示例配置
location /titanium-stainless-jewelry { rewrite ^/.* https://$server_name/stainless-stainless-jewelry permanent; }
路徑修改重定向
pre.weifeng.com/en/ 開始路徑url 重定向到 pre.weifeng.com路徑
location ~ /en { rewrite ^/en/(.*) https://pre.weifeng.com/$1 permanent; }