【问题标题】:Nginx rewrite urls with special charactersNginx 用特殊字符重写 url
【发布时间】:2017-08-31 08:35:59
【问题描述】:

我刚刚得到了一个需要重写到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 {...}

【问题讨论】:

    标签: nginx nginx-location


    【解决方案1】:

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

    实现目标的一种方法是使用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 了解更多信息,以及this caution 了解if 的使用。

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多