【问题标题】:nginx redirect to new url if script name is only contain numbers如果脚本名称仅包含数字,则 nginx 重定向到新 url
【发布时间】:2016-12-26 06:48:46
【问题描述】:

如果脚本名称仅包含数字,我该如何重定向?

http://example.com/125689 -> http://example.com/news/125689

我使用了此代码,但出现 404 错误。

location ~ /([0-9]+) {
    return 301 http://example.com/news/$1;
}

【问题讨论】:

    标签: wordpress nginx nginx-location


    【解决方案1】:

    您创建了一个重定向循环,因为您的正则表达式未锚定到行首,因此它也匹配重写的 URI。

    您可以通过添加^$ 来限制正则表达式的范围。

    location ~ ^(/\d+)$ {
        return 301 $scheme://$host/news$1;
    }
    

    有关正则表达式的有用资源,请参阅 this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 2012-10-05
      • 2014-02-11
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多