在 Nginx 中修剪路徑並重新導向

在 Nginx 中修剪路徑並重新導向

我有一個 WordPress 伺服器,www.mydomain.com/A/B Nginx 配置如下:

server {
    listen 80 default;

    root /var/www/html;

  location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }


    location /A/B {
        try_files $uri $uri/ /A/B/index.php?$args;
    }
...
}

這工作正常。

我現在想做的是將舊路徑重新導向到新路徑。

基本上我想要www.mydomain.com/A/B/C/XXX/YYY/ZZZ--> www.mydomain.com/A/B/XXX/YYY/ZZZ。正在刪除/C

我相信我可以做到這一點:

location /A/B/C {
            try_files $uri $uri/ /A/B/index.php?$args;
}

但這沒有用。然後我嘗試了

location /A/B/C {
    proxy_pass http://localhost/A/B;  # note the trailing slash here, it matters!
}

我想我可能需要另一種方式,因為我/XXX/YYY需要/C.

任何幫助表示讚賞。謝謝。

答案1

您需要重寫 URL 以刪除C路徑元素。

例如:

rewrite ^(/A/B/)C/(.*)$ $1$2 permanent;

您可以將其放置在server區塊中或location ^~ /A/B/C/區塊中。

相關內容