Nginx 用特殊字符重写 url
Nginx rewrite urls with special characters
我刚刚得到一个需要重写到 nginx 中的 url 列表
example.com/cms.php/inspiration?fullsize > /
example.com/shop/?category_id=27 > /garden/cushion.html
example.com/shop/?2008=1&vare_id=10553&variant_id=2232 > /garden/cushion/black.html
我尝试了以下
简单重写
rewrite /shop/?category_id=27 /garden/cushion.html permanent;
位置重写
location /shop/?category_id=27 {
rewrite /shop/?category_id=27 /garden/cushion.html;
}
还有我评论特殊标志的位置
location /shop/\?category_id=27 {
rewrite /shop/\?category_id=27 /garden/cushion.html;
}
我直接在nginx的配置server {...}
中添加
首先,从 ?
开始的任何内容都是 查询字符串 ,而不是 rewrite
和 [=13] 使用的规范化 URI 的一部分=] 指令。
实现 objective 的一种方法是使用 map
指令测试 $request_uri
变量。
例如:
map $request_uri $redirect {
default 0;
/cms.php/inspiration?fullsize /;
/shop/?category_id=27 /garden/cushion.html;
/shop/?2008=1&vare_id=10553&variant_id=2232 /garden/cushion/black.html;
}
server {
...
if ($redirect) { return 301 $redirect; }
...
}
参见 this document for more, and this caution 关于 if
的使用。
我刚刚得到一个需要重写到 nginx 中的 url 列表
example.com/cms.php/inspiration?fullsize > /
example.com/shop/?category_id=27 > /garden/cushion.html
example.com/shop/?2008=1&vare_id=10553&variant_id=2232 > /garden/cushion/black.html
我尝试了以下
简单重写
rewrite /shop/?category_id=27 /garden/cushion.html permanent;
位置重写
location /shop/?category_id=27 {
rewrite /shop/?category_id=27 /garden/cushion.html;
}
还有我评论特殊标志的位置
location /shop/\?category_id=27 {
rewrite /shop/\?category_id=27 /garden/cushion.html;
}
我直接在nginx的配置server {...}
中添加
首先,从 ?
开始的任何内容都是 查询字符串 ,而不是 rewrite
和 [=13] 使用的规范化 URI 的一部分=] 指令。
实现 objective 的一种方法是使用 map
指令测试 $request_uri
变量。
例如:
map $request_uri $redirect {
default 0;
/cms.php/inspiration?fullsize /;
/shop/?category_id=27 /garden/cushion.html;
/shop/?2008=1&vare_id=10553&variant_id=2232 /garden/cushion/black.html;
}
server {
...
if ($redirect) { return 301 $redirect; }
...
}
参见 this document for more, and this caution 关于 if
的使用。