【问题标题】:Nginx - Wordpress redirect to default urlNginx - Wordpress 重定向到默认网址
【发布时间】:2018-07-17 16:58:07
【问题描述】:

我有一个关于重定向的问题,我想重定向到 example.com/12321.html 到 example.com?p=12321。

我是 url 类型之前的用户,比如第一个数字,之后我将系统更改为 seo url,内容名称为 url。现在我将最好的系统重定向到 ?p=

我的尝试

首先我尝试在 .htaccess 不工作时更改它我使用此代码

RewriteEngine On
RewriteRule "^([0-9]+)+[^.html]" "https://www.example.com?p=$1" 

比我想用 nginx 在 nginx 服务器块上写这段代码

rewrite ^([0-9]+)+[^.html] https://www.example.com/$1 redirect;

我不明白这就是为什么不工作,我已经在https://regex101.com/r/pimHGN/1 上查看过,显示在那里工作。

【问题讨论】:

    标签: php wordpress .htaccess redirect nginx


    【解决方案1】:

    好的,作为快速检查,您可以为 Apache 尝试如下的正则表达式:

    RewriteRule ^/([0-9]+)\.html$ https://www.example.com?p=$1 [L,R=301]
    

    对于 Nginx 也是这样:

    rewrite ^/([0-9]+)\.html$ https://www.example.com?p=$1 permanent;
    

    这应该匹配任何由数字0-9.html 组成的页面。

    您发布的规则与 .html 部分不匹配,这可能是它不起作用的原因。

    【讨论】:

    • 谢谢你的回答,但它不工作我现在检查它:(
    • 我的错,忘记了捕获组和前导斜线。
    【解决方案2】:

    不太清楚您要做什么,但是,除非您有一个 rewrite 规则,该规则首先从 $uri 中删除 /,否则您的 rewrite ^([0-9]+)+[^.html] https://www.example.com/$1 redirect; 规则将永远不会匹配,因为它$uri 不可能不以 / 开头,因此,^([0-9]+) 永远不会捕获任何东西。 (此外,[^.html] 看起来也很可疑——我感觉它不会像你预期的那样做。)

    正确的方法可能是:

    rewrite ^/([0-9]+)\.html$ /?p=$1 redirect;
    

    但是,根据 nginx redirect loop, remove index.php from url 和相关的,以下“循环”可能是您真正想要的:

    if ($request_uri ~ ^/?p=([0-9]+)) {
        return 302 /$1.html
    }
    rewrite ^/([0-9]+)\.html$ /?p=$1;
    

    上述代码将确保 /?p= 样式仅在 nginx 和后端内部使用,而在外部,所有内容始终被重定向到 .html 样式页面并从中提供服务。

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多