Reputation: 2834
I inherited a legacy site and for SEO reasons wish to redirect old parameter links to the new api. Like so:
www.example.com/section/?page=3 => www.example.com/page/3
Here is my attempt at a regex redirect:
location ~ ^/section\/\?page=(.*)/$ {
return 301 https://www.example.com/page/$1;
}
I've used this pattern countless times without issue but after hours of poking I still can't get the redirect working. Clearly Nginx treats parameters differently.
Any tips?
Upvotes: 0
Views: 48
Reputation: 5951
You don't need regex here, it would add unnecessary processing overhead anyway.
Nginx captures url parameters already. you can use the $args
variable for all the parameters, or individual ones are prefixed $arg_
, so assuming all your urls match this format you can just use a location block like this:
location /section/ {
return 301 https://www.example.com/page/$arg_page;
}
Upvotes: 1
Reputation: 25321
You have a trailing slash in your regex, so it would only match www.example.com/section/?page=3/
.
Try ^/section\/\?page=(.*)$
Upvotes: 0