How can I do 301 Redirect in nginx for 200 URLs?

I have 200 URLs that I need to redirect to new URLs e.g. -----> -----> ----->

On Apache it was very simple to do in .htaccess like:

Redirect 301 /one/p-SF10
Redirect 301 /one/p-SM28
Redirect 301 /onep-KH83

I found one of the guides with following lines:

if ( $request_filename ~ oldpage/ ) {
rewrite ^ permanent;

My question here is do I need to create these 200 ‘if conditions’? Wouldn’t it create a huge conditional loop and overload my server?

Server configuration: I installed Nginx+php-fpm using VestCP and set the nginx template to wordpress from within VestCP which configured my nginx.conf automatically.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

You are right to want to avoid using if conditions here, luckily you can avoid them completely by doing the matching with a regex right in the rewrite directive. For example:

rewrite ^/path/to/old $scheme://$host/new/url permanent;

This blog post from Nginx themselves is a good read on the different ways to create rewites and redirects with Nginx and when to use them.