【问题标题】:Rewrite with 404 urls with nginx使用 nginx 重写 40​​4 url
【发布时间】:2016-01-02 13:25:51
【问题描述】:

我正在尝试重写一些显示为 404 的 url,但我无法让重写工作。网址看起来像这样/ossobuco-alla-milanese​​/1451114854360.1451114854360?time=1451114851111。我想通过重写删除1451114854360.1451114854360?time=1451114851111

在我的 nginx 配置中,我有以下重写规则

rewrite "^\/(.*)\/(\d{13}\.\d{13}\?time=\d{13})$" /$1/ permanent;

我在 2 个在线正则表达式工具 regex101regex pal 中测试了正则表达式,它应该可以工作,但似乎在我的服务器上不起作用。

【问题讨论】:

    标签: regex nginx url-rewriting rewrite


    【解决方案1】:

    要匹配查询字符串,请使用$args:

    location / {
        if ($args ~* "^time=\d+") {
            set $args '';
            rewrite "^/(.+)/\d+\.\d+/?$" /$1 permanent;
        }
    }
    

    PS:如果您只想匹配13digits.13digits,请使用:

    rewrite "^/(.+)/\d{13}\.\d{13}/?$" /$1 permanent;
    

    【讨论】:

    • 我试过了,它把网址改写为domain.tld而不是domain.tld/ossobuco-alla-milanese​​/
    • Namaste anubhava!你知道我们可以交谈的网站吗?我需要你的建议,你能帮忙吗?。
    • Dhanyavaad Anubhava 先生,我三年来从未在这个网站上创建过聊天室。但我会尽力让你知道。谢谢阿努巴瓦爵士,对不起我可怜的印地语,我很想学习它。我正在学习它。
    • 我可以知道为什么您在提交答案后这么久没有接受吗?您自己的答案并不能确保查询字符串包含 time=
    【解决方案2】:

    最后它只是一个非常简单的位置块和?删除参数。

    location ~* ^/(.+)/\d+\.\d+$ {
        rewrite ^/(.+)/\d+\.\d+$ /$1? permanent;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-24
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 2016-02-07
      相关资源
      最近更新 更多