【问题标题】:Nginx Rewrite Encoded URI (with percent) Not WorkingNginx 重写编码的 URI(带百分比)不工作
【发布时间】:2021-06-20 15:30:18
【问题描述】:

我有一个 WordPress 网站,需要 301 将旧帖子重定向到新帖子。

旧帖:

https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/

新帖子:

https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/

我在 nginx.conf 中为这个域添加了这个规则

server
{
    listen 111.222.333.444:80;
    server_name example.com www.example.com ;
    return 301 https://www.example.com$request_uri;
}

server
{
    rewrite_log on;

    rewrite ^/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/$ https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/ permanent;

    location / {
      # This is cool because no php is touched for static content.
      # include the "?$args" part so non-default permalinks doesn't break when using query string
      try_files $uri $uri/ /index.php?$args;
    }

    (the rest of location blocks continue)
}

重启 Nginx。 但是,旧 URL 仍然返回 404 而不是 301。

https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/

而且我在错误日志中根本看不到旧的或新的 URI。我该怎么办?谢谢!

【问题讨论】:

    标签: nginx redirect url-rewriting


    【解决方案1】:

    百分比编码的 URL 在 $request_uri 变量中可用。但是当 Nginx 处理rewritelocation 语句时,URL 已经是decoded and normalised

    使用带有解码值的rewritelocation 语句。例如:

    rewrite ^/สวัสดี/$ /ลาก่อน/ permanent;
    

    或者:

    location = /สวัสดี/ {
        return 301 /ลาก่อน/;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-23
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 2015-09-24
      相关资源
      最近更新 更多