![nginx 將 /2013/04/test.html 重定向到 /test - 不起作用](https://rvso.com/image/623652/nginx%20%E5%B0%87%20%2F2013%2F04%2Ftest.html%20%E9%87%8D%E5%AE%9A%E5%90%91%E5%88%B0%20%2Ftest%20-%20%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我一直在為這件事精神崩潰。如何在 nginx 中/2013/04/test.html
重定向某個 url?/test
我已經嘗試過這個:但不起作用:
server {
location /2013/05/test.html {
return 301 http://$server_name/test;
}
}
我已經執行了一些測試 - 由於某種原因,配置行的位置部分中沒有 .html 擴展名的任何 url 都會正確重定向,但是一旦我將 .html 放置在位置 kaboom 中,它就會停止工作。
知道這是為什麼嗎?謝謝你!
答案1
您始終可以將重寫規則新增至現有位置區塊中,而不是為每個重定向設定一個位置區塊:
rewrite /2013/05/test.html http://$server_name/test permanent;
rewrite /2013/05/test2.html http://$server_name/test2 permanent;
您也可以執行各種正規表示式,這樣就可以避免每次新增新的「永久連結」時都必須新增新的正規表示式。
答案2
我同意 Jason Ilicic 的觀點,使用重寫規則可能會更有效率。
如果你真的想使用位置區塊,您嘗試過使用“=”修飾符嗎?如果您有多個位置,Nginx 選擇邏輯可能會選擇與您期望不同的位置。
來自 Nginx 文檔
using the “=” modifier it is possible to define an exact match of URI and location
例如
server {
location = /2013/05/test.html {
return 301 http://$server_name/test;
}
}