停止 Nginx 重寫破壞位置區塊後面的所有頁面

停止 Nginx 重寫破壞位置區塊後面的所有頁面

我必須重寫 nginx.conf 中包含特定查詢參數的 URL;

舉個例子:-

location /brands/exampleA {

    if ($arg_cat = "9") {
        return 301 /page/brand-filter;
    }

    if ($arg_cat = "38") {
        return 301 /page/category/brand-filter;
    }

}

然後,這些 URL 重寫將重寫example.com/brands/exampleA/?cat=9example.com/page/brand-filter和。example.com/brands/exampleA/?cat=38example.com/page/category/brand-filter

這些工作完美,但問題是它們破壞了位置區塊的所有其他子頁面,因此例如,以下頁面都不會載入並出現 Nginx 錯誤:-

example.com/brands/exampleA/range1
example.com/brands/exampleA/range2
example.com/brands/exampleA/range3
example.com/brands/exampleA/range4

那麼,我可以在位置語句中添加一些內容來阻止任何應用於之後的任何內容exampleA- 這些重寫應該僅匹配 ?cat= 查詢參數。

答案1

您的配置目前使用前綴位置,這意味著當請求的 URI 時被考慮開始於價值/brands/exampleA

若要將符合限制為僅一個 URI,請使用完全符合句法:

location = /brands/exampleA/ { ... }

這個文件了解詳情。

相關內容