Sub topic 16
Planning redirects for migrations and URL changes
This chapter assumes you know the redirect types (301, 302, 307, 308), can read curl output, and can edit nginx or Apache configuration. It covers how to plan redirects before a URL change, so the change doesn't create 404s and redirect chains.
Why migrations need a plan
A migration can be a new domain, a switch to HTTPS, a CMS change, or a new URL structure. In each case, every old URL that has traffic or inbound links must lead to its new equivalent. If you fix redirects after launch, users and crawlers hit 404s in the meantime.
Step 1: Inventory the old URLs
Collect URLs from several sources, because no single one is complete:
- A crawl of the current site.
- The XML sitemap.
- Server logs, which show URLs that real users and bots request.
- Search Console and backlink reports, which show URLs that other sites link to.
Step 2: Build the redirect map
A redirect map is a table of old URL and new URL. Follow these rules:
- Map each old URL to the most relevant new page, not to the home page. Mass redirects to the home page are often treated as soft 404s.
- Point to the final URL, never to another redirected URL. This prevents chains.
- Use 301 or 308 for permanent moves.
- If content has no replacement, leave it out of the map and return 410 or 404.
Keep the map in a file such as redirect-map.csv:
/old-page,/new-page
/blog/2019/post,/articles/post
/products/widget-1,/shop/widgetStep 3: Implement it
In nginx, a map block keeps hundreds of redirects out of your server block. This example lives in the http context:
http {
map $uri $redirect_target {
default "";
/old-page /new-page;
/blog/2019/post /articles/post;
/products/widget-1 /shop/widget;
}
server {
listen 80;
server_name example.com;
if ($redirect_target) {
return 301 $redirect_target;
}
}
}For larger maps, move the pairs into a file and use include inside the map block.
Step 4: Test every pair
Check that each old URL ends at the expected page with the fewest hops:
while IFS=, read -r old new; do
result=$(curl -s -o /dev/null -L \
-w '%{num_redirects} hops, status %{http_code}, final %{url_effective}' \
"https://example.com$old")
echo "$old -> $result (expected $new)"
done < redirect-map.csvEach line should show 1 hop, status 200, and a final URL ending in the expected path. Two or more hops means a chain. A status of 404 means the target is missing.
Step 5: Monitor after launch
For several weeks after launch, check these:
- Search Console reports for new 404s and soft 404s.
- Server logs for 404 requests, using the awk command from the log-analysis chapter.
- A repeat crawl to catch internal links that still point to old URLs.
Keep migration redirects for at least a year. Keep them longer if old URLs still receive links or traffic.
Exercise
Your map redirects /a to /b. Later, /b is renamed to /c and you add a redirect from /b to /c. What problem does this create for /a, and how do you fix it?
Show answer
/a now takes two hops (/a to /b to /c), which is a redirect chain. Fix it by updating the /a entry to point straight to /c, and update internal links to use /c.