Score:1

Trimming the path and redirect in Nginx

cn flag

I have a Wordpress server at www.mydomain.com/A/B The Nginx config is as:

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;
    }
...
}

This is working fine.

What I want to do now is to redirect a legacy path to the new path.

Basically I want www.mydomain.com/A/B/C/XXX/YYY/ZZZ --> www.mydomain.com/A/B/XXX/YYY/ZZZ. Removing /C.

I believe I could do it with:

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

But it didn't work. Then I tried

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

I think I maybe need another way since I need the /XXX/YYY path after the /C.

Any help appreciated. Thank you.

djdomi avatar
za flag
if the path is gone, consider to use `return 301 https://newpath;`
Score:0
jp flag

You will need to rewrite the URL to remove the C path element.

For example:

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

You can place this in the server block, or in a location ^~ /A/B/C/ block.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.