Nginx 用特殊字符重写 url [英] Nginx rewrite urls with special characters

查看:126
本文介绍了Nginx 用特殊字符重写 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚得到一个需要重写到 nginx 的 url 列表

I just got a list of urls that needs to be rewritten into nginx

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;
}

还有我评论特殊标志的位置

And with location where I commented special signs

location /shop/?category_id=27 {
    rewrite /shop/?category_id=27 /garden/cushion.html;
}

我直接将它们添加到nginx配置中的server {...}

I add them directly into server {...} in the nginx configuration

推荐答案

首先,从 ? 开始的任何东西都是查询字符串,而不是规范化 URI 的一部分由 rewritelocation 指令使用.

Firstly, anything from the ? onwards is the query string and is not part of the normalised URI used by the rewrite and location directives.

实现目标的一种方法是使用 map 指令测试 $request_uri 变量.

One way to achieve your objective is to test the $request_uri variable using a map directive.

例如:

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; }
    ...
}

查看本文档了解更多信息,这个警告关于if的使用.

See this document for more, and this caution on the use of if.

这篇关于Nginx 用特殊字符重写 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆