Nginx 重写编码 URI(带百分比)不工作

Nginx Rewrite Encoded URI (with percent) Not Working

我有一个 WordPress 站点需要将旧的 post 301 重定向到新的 post。

旧 post:

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

新 Post:

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 仍然是 return 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。我应该怎么办?谢谢!

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

对解码值使用 rewritelocation 语句。例如:

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

或者:

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