【问题标题】:Nginx URL rewrite search?q=string (opencart)Nginx URL 重写搜索?q=string (opencart)
【发布时间】:2012-07-21 18:12:29
【问题描述】:

我正在努力

http://site.com/index.php?route=product/search&filter_name=QUERY

http://site.com/search?=QUERY

这是我的规则;它不起作用。

rewrite ^/search?q=(.*)?$ /index.php?route=product/search&filter_name=$1 last;

【问题讨论】:

    标签: .htaccess url url-rewriting nginx opencart


    【解决方案1】:

    Nginx 不允许你匹配查询字符串,而是有一个$args 变量和if -s。您可以将其结合起来,使示例像这样工作:

    (使用 nginx if else 语句更新)

    location /search {
        if ($args ~ "q=(?<q>.*)?") {
            rewrite ^ /index.php?route=product/search&filter_name=$q last;
        }
        # Else (if no search query entered from form)
        rewrite ^ /index.php?route=product/search last;
    }
    

    但由于 if -s are considered evil 在 nginx 配置中,您应该使用(在您的情况下)try_files

    location /search
        try_files $uri /index.php?route=product/search&filter_name=$arg_q;
    }
    

    【讨论】:

    • 感谢您的评论。但是我已经更新了我的帖子。你能改变你的答案吗
    • 更新了,不幸的是try_files必须被删除,但只要你使用rewrite ... last;它应该是安全的。
    • try_files 因不支持更复杂的字符串而被删除?谢谢你的回答我真的很感激。
    • 是的,通过在if 块下添加rewrite ^.*$ /index.php?route=whereever last;
    • ^.*$ - 是在浪费 CPU。 ^ - 就够了。
    【解决方案2】:

    问题在于$ 符号。在这种情况下需要事先。解决方案:

    rewrite ^/search?q=$(.*)$ /search.php?q=$1? last;
    

    【讨论】:

    • 感谢您的评论。但是我已经更新了我的帖子。你能改变你的答案吗
    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 2013-07-08
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多