Forem Creators and Builders 🌱

Varhal
Varhal

Posted on

301 redirect with Nginx map module

Hi! I have a large wordpress project with thousands of pages. I want to migrate it to Forem. We wrote a migration script for that using Forem API. I have to do a mass 301 redirect from old pages to new ones to not loose positions in search engines.

Google has suggested that the best way to do mass redirects is to use Nginx map module. At first glance, everything there is simple. Create a file with redirects, plug it into the Nginx file and create rules.

First, i create a new file:

/opt/forem/configs/regex.map

File regex.map:

/old /varhal/podborka-topovykh-biesplatnykh-sietov-ikonok-dlia-dizainiera-1e4b;

After i edited nginx.conf:

/opt/forem/configs/nginx.conf

Include map file:

map $request_uri $new_uri {
include /opt/forem/configs/regex.map;
}

Add code to server section (variant 1):

server {
...
if ($new_uri != "") {
rewrite ^(.*)$ $new_uri permanent;
}
...
}

Add code to server section (variant 2):

server {
...
if ($new_uri) {
return 301 $new_uri;
}
...
}

After all the manipulation, different versions of code and errors redirect did not work. When you go to a page that should be redirected, there is a wait of several minutes and redirect does not work. In some cases when I redirected to the main page, after the redirect the page looked like this :
example.com:9090 and it didn't work.

Need help from the community, thanks!

Use this tutorials:

https://gauravswaroop.medium.com/nginx-bulk-url-redirect-with-map-47c2cd6ad50a

https://stackoverflow.com/questions/29354142/nginx-how-to-mass-permanent-redirect-from-a-given-list

Top comments (3)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Thanks for bringing this up! I'm sure others will be facing similar migration troubles. I can confirm that I've flagged this to the team and hopefully we can get you some advice here soon. 🀞

Collapse
 
michaeltharrington profile image
Michael Tharrington

Hey again @varhal πŸ‘‹

So, I spoke to a team member who is more technically savvy than myself, but ultimately, they noted that that diagnosing this from the outside is going to be very difficult. Really the best we could do is try to create a similar setup and see what problems we encounter.

I'm sorry that I can't give you more advice on this front. I hope this doesn't discourage you too much from continuing with your migration because I really want you to succeed here.

If anyone else from the team weighs in with more info, I'll be sure to deliver the news to ya!

Collapse
 
varhal profile image
Varhal

Resolved in this post: forem.dev/varhal/301-redirect-afte...