How to Redirect Old URLs to New Ones in Nginx Without Breaking SEO

Every site restructure leaves old URLs behind, and every old URL that returns a 404 leaks the ranking value it built up over months or years. Learning how to redirect old URLs in Nginx properly is what keeps that value moving to the new pages instead of vanishing. A redirect is a simple instruction, but the difference between a clean 301 and a sloppy chain is the difference between keeping your traffic and watching it slide.

What a 301 Redirect Does (and Why 302 Is the Wrong Default)

A 301 redirect tells browsers and search engines that a page has moved permanently. Google passes most of the old page’s ranking signals to the new URL, and browsers cache the redirect so repeat visitors go straight to the new address. A 302 redirect says the move is temporary, so search engines keep the old URL in the index and hold back on passing ranking signals. That is the right choice for a page that is down for maintenance or an A/B test, and the wrong choice for a URL that will never come back. This is the simplest form of an nginx 301 redirect, and it works for any single page move. If you are restructuring a site, use 301 for every permanent move and reserve 302 for genuinely temporary ones. Mixing them up is one of the most common reasons a migration loses traffic even though every link still works.

How to Redirect Old URLs in Nginx with return 301

For a single page moving to a new address, the return directive is the cleanest tool Nginx offers. It stops processing the request immediately and sends the redirect, which keeps things fast and easy to read. Add a location block inside your server block:

location = /old-page/ {
    return 301 https://example.com/new-page/;
}

The = makes this an exact match, so only that one URL triggers the redirect. Everything else passes through untouched. If the new page lives on the same site and you want to keep the scheme and host flexible, use a relative target instead:

location = /old-page/ {
    return 301 /new-page/;
}

After editing the config, test it with nginx -t and reload with systemctl reload nginx. Never skip the test step. A typo in a server block can take the whole site down, and a redirect is not worth an outage.

Redirecting a Whole Folder with a rewrite Rule

Single-page redirects get tedious when an entire section moves, such as /blog/ becoming /news/. That is where an nginx rewrite rule earns its keep. A regex capture grabs the part after the folder name and carries it to the new location:

location /blog/ {
    rewrite ^/blog/(.*)$ /news/$1 permanent;
}

The keyword permanent makes this a 301. Use redirect instead if you ever need a temporary 302 version of the same rule. One warning: regex locations behave differently from prefix locations, and a badly written pattern can match URLs you never meant to touch. If you are unsure how Nginx picks between location types, read our guide to how Nginx location matching works before you write the rule. Test each pattern against a few real URLs, including ones that should not match.

Moving to a New Domain

When the whole site changes domain, you do not need a rule per page. One server block catches every request to the old domain and sends it to the same path on the new one:

server {
    listen 80;
    server_name old-domain.com www.old-domain.com;
    return 301 https://new-domain.com$request_uri;
}

The $request_uri variable carries the full path and query string, so /pricing/?plan=pro on the old domain lands on /pricing/?plan=pro on the new one. Keep this block live for at least a year. Old links from other sites, bookmarks, and search results keep pointing at the old domain for a long time, and removing the redirect too early turns all of them into dead ends. If the domain move also involves HTTPS, sort out the certificate for the old domain first, because a redirect cannot happen before the TLS handshake succeeds. Our walkthrough of redirecting HTTP to HTTPS in Nginx covers the certificate and listener setup that pairs with this.

Redirect Chains and Loops That Quietly Drain Rankings

A redirect chain happens when URL A points to B, and B points to C. Browsers follow the chain, but each hop costs time and dilutes the ranking signals passed along. Google follows a limited number of hops before giving up, so a long chain can leave the final page effectively unlinked. The fix is simple but easy to forget: whenever you add a new redirect, check whether its target is itself redirected, and point the old URL straight at the final destination. A loop is worse. URL A points to B and B points back to A, and the browser spins until it errors out. Loops usually come from two rules written at different times by different people, or from a redirect that conflicts with a canonical tag or an application-level redirect in WordPress. If a page suddenly reports too many redirects, our guide to the Nginx too many redirects error walks through the usual suspects in order. As a habit, keep a single list of all your redirect rules in one place, whether that is a dedicated include file or a section at the top of the server block, so a future edit does not quietly create a chain.

Test Every Redirect Before You Reload

A redirect that looks right in the config can still fail in practice, so verify it from the outside with curl:

curl -I https://example.com/old-page/

You want to see HTTP/2 301 and a location header pointing exactly at the new URL. Then follow it with curl -IL to confirm the final page returns 200 and the path has no extra hops. Check a few edge cases too: the trailing-slash version, the version without a trailing slash, and any URL with a query string. A redirect that drops query strings can break tracking links and shared URLs that depend on them. For bigger changes, stage the config on a test server or a staging copy of the site first. The process in testing a new Nginx config without taking your site down shows how to validate a config and roll back fast if something breaks.

Tell Google the Move Happened

Redirects alone do not update Google’s index overnight. After the rules are live, update your XML sitemap so it lists only the new URLs, and submit it in Search Console. Old URLs will keep appearing in reports for a while as Google recrawls them and follows the redirects. That is normal. The redirect old url SEO payoff comes when the new pages start ranking for the queries the old ones held. What is not normal is an old URL still showing in the sitemap weeks later, which tells Google the old address is still a real page. Update every internal link on your own site to point at the new URLs as well. Redirects are a safety net for links you do not control, not a replacement for fixing the links you do. Check your Search Console coverage report a week or two after the move: the old URLs should show as redirected, and the new ones should be indexed and getting impressions.

Frequently Asked Questions

Should I redirect every old URL, or just the important ones?

Redirect every URL that had traffic, backlinks, or rankings. For old URLs nobody visited and nobody linked to, letting them return a 404 is fine and keeps your config clean.

How long should I keep a 301 redirect in place?

At least a year, and permanently if the old URL has backlinks. Removing a redirect resurrects every dead link pointing at the old address.

Can I use a wildcard redirect for a whole site?

Yes, if every page maps to the same path on the new domain, as in the domain-move example above. If the URL structure changed too, you need per-page or per-pattern rules instead.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.