简体   繁体   中英

Nginx rewrite subfolder to subfolder

I have been having issues redirecting an old subfolder entirely to a new subfolder. I need to have both /old and /old/ go to /new and /new/ respectively

I also need to have any parameters followed after /old/blah/blah2/ to /new/blah/blah2/ so just basically replacing old with new no matter whats called. The below is the closest I can get.

    location /account/ {
       rewrite ^/account/(.*)$ https://$server_name/portal/$1 permanent;
     }

Example of an actual URL:

www.domain.com/account/index.php?loend=true&cmd=callback&module=8 needs to be www.domain.com/portal/index.php?loend=true&cmd=callback&module=8

Thank you

Can you try rewrite ^/account/(.*)$ /portal/$1; ? No need to put it into location /account/ { ... } – Guillaume Filion Jul 24 at 19:00

Using just: rewrite ^/account/(.*)$ /portal/$1; without specifying the location has resolved all issues

Its should work but you're missing $ sign in it. Here is corrected a bit code

rewrite ^/account/(.*)$ https://$server_name/portal$1 redirect;

or

rewrite ^/account/(.*)$ https://$server_name/portal$1 last;

or

rewrite ^/account/(.*)$ https://$server_name/portal$1;

Than reload config of nginx

service nginx reload

here is source site.

https://www.digitalocean.com/community/questions/301-redirect-nginx

Since this seems more of a redirect than a rewrite, I would use return

location ^~ /account(.*) {
  return 301 https://$server_name/portal$1$is_args$query_string;
}

The $is_args$query_string is to append any query string like you mentioned in one of the comments loend=true&cmd=callback&module=8 and also mind that if the $server_name is the same name of server_name you can replace it with $http_host and keep it dynamic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM