nginx如何將網址重定向到特定頁面


我看過這個問題,該問題描述了如何在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\indextest.com\hometest.com\maintest.com\index.php

然后,我還有其他頁面可以簡單地重定向到.php擴展名

例如test.com\about\test.com\about.php

 

做這個的最好方式是什么?
找到了答案…假設以下服務器塊 test.com

server { listen 80; server_name www.test.com; ... } 

 

將適當的正則表達式位置路徑添加到重定向URL ,rewritereturn將其添加到重定向URL。

test.com\indextest.com\hometest.com\maintest.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;  
   }

  




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM