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

查看:842
本文介绍了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

推荐答案

首先,从?开始的所有内容都是查询字符串,而不是location指令.

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天全站免登陆