大网数据广告活动
服务器运维问答

Stop Nginx rewrites breaking all pages following location block

· 52CE 我爱测

I'm having to rewrite URLs in nginx.conf that contain particular query parameters; As an example:- location /brands/exampleA { if ($arg_cat = "9") { return 301 /page/brand-filter; } if ($arg_cat = "38") { return 301 /page/categor…

问题原文

原帖链接 · zigojacko · CC BY-SA 4.0

I'm having to rewrite URLs in nginx.conf that contain particular query parameters;

As an example:-

location /brands/exampleA {

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

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

}

These URL rewrites would then rewrite example.com/brands/exampleA/?cat=9 to example.com/page/brand-filter and example.com/brands/exampleA/?cat=38 to example.com/page/category/brand-filter.

And these work perfectly, but the problem is that they break every other child page of the location block so for example, the following pages would all not load with an Nginx error:-

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

So is there something I can add to the location statement to stop anything applying to anything after exampleA - these rewrites should ONLY match ?cat= query parameters.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

Your configuration currently uses a prefix location, which means that it is considered when the requested URI begins with the value /brands/exampleA.

To restrict the match to only one URI, use the exact match syntax:

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

See this document for details.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-03-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。