Sub topic 10
Redirect Old URLs with 301s
This chapter assumes you can read Search Console's Pages report, edit server configuration (or ask someone who can), and run a short Python script. The previous chapter covered merging overlapping pages into one. Here you make the old URLs lead to the surviving page, so that visitors, crawlers and link equity follow.
What a 301 does
A 301 is an HTTP response with status code 301 Moved Permanently and a Location header holding the new URL. It tells crawlers that the old URL has been replaced for good. Google treats a permanent redirect as a strong signal that the target should be the canonical URL. Over time it drops the old URL from the index and credits the target with the signals the old URL had earned, including links from other sites.
| Status | Meaning | Use for consolidation? |
|---|---|---|
| 301 | Moved permanently | Yes, the default choice |
| 308 | Permanent, request method preserved | Yes, equivalent for search purposes |
| 302 / 307 | Temporary | No. Search engines may keep the old URL indexed |
| 410 | Gone on purpose | For pruned pages with no replacement |
| 404 | Not found | Acceptable for pruned pages, but 410 is more explicit |
Meta refresh and JavaScript redirects are slower to process and less reliable. Use a server-side redirect whenever you control the server.
Choose the right target
The target decides whether a redirect works. Pick it with these rules:
- Send each old URL to the page that now answers the same query. If you merged
/blog/seo-tips-2019/into/guides/seo-checklist/, redirect there. - Do not redirect everything to the homepage. Google treats a redirect to an unrelated page as a soft 404. The old URL's equity is then lost, and visitors land on a page that does not match what they clicked.
- Redirect straight to the final URL. Avoid chains such as old to intermediate to final. Each hop adds latency and risks being dropped. Google follows a limited number of hops, and chains also waste crawl budget.
- Make sure the target returns 200, is indexable (no
noindex), and is not itself redirected elsewhere. - If no relevant target exists, the page should not be redirected. Return 410 or 404, as covered in the pruning chapter.
Implement the redirects in nginx
For one page, a location block is enough. For several pages, a map keeps the list in one place and is easier to review and keep in version control.
# In the http block: exact-match map of retired paths to their targets
map $uri $merged_target {
default "";
/blog/on-page-seo-basics/ /guides/seo-checklist/;
/blog/seo-tips-2019/ /guides/seo-checklist/;
}
server {
listen 80;
server_name example.com;
# An empty string is false, so only mapped URLs are redirected
if ($merged_target) {
return 301 $merged_target;
}
}Points to check in this configuration:
mapis only valid in thehttpblock, not insideserver.- The match is on
$uri, which excludes the query string. A request for/blog/seo-tips-2019/?utm_source=xstill matches, and nginx drops the query string unless you append$is_args$argsto the target. - Matching is exact, including the trailing slash. If both
/blog/seo-tips-2019and/blog/seo-tips-2019/can be requested, list both or normalize one into the other first. ifinsideserverwith onlyreturnis a safe use. Avoid putting other directives insideif.
On Apache, the equivalent is Redirect 301 /old/ /new/ or a RewriteRule with [R=301,L]. On a CMS, a redirect plugin does the same job, but check the status code it sends, because some default to 302.
Handle site-wide variants too
The same logic applies to the other ways a page can be requested:
httpshould redirect tohttps.wwwand the bare domain should redirect to whichever you chose.- Trailing-slash and non-trailing-slash forms should resolve to one.
Make these redirects land on the final URL in a single hop. A common mistake is http://www.example.com/old redirecting to https://www.example.com/old, then to https://example.com/old, then to the new page. Three hops can be reduced to one by mapping the old path to the new path in every server block that receives that traffic.
Verify every redirect
Do not trust the configuration file. Request each old URL without following redirects and check the status, the target, and the target's own status. This script uses only the standard library:
import http.client
import sys
from urllib.parse import urlsplit
HOST = "example.com"
REDIRECTS = {
"/blog/on-page-seo-basics/": "/guides/seo-checklist/",
"/blog/seo-tips-2019/": "/guides/seo-checklist/",
}
def head(path):
"""Return (status, location) without following redirects."""
conn = http.client.HTTPSConnection(HOST, timeout=10)
try:
conn.request("HEAD", path)
resp = conn.getresponse()
return resp.status, resp.getheader("Location")
finally:
conn.close()
def main():
problems = []
for old, expected in REDIRECTS.items():
status, location = head(old)
if status != 301:
problems.append(f"{old}: expected 301, got {status}")
continue
parts = urlsplit(location or "")
if parts.netloc and parts.netloc != HOST:
problems.append(f"{old}: redirects to another host {parts.netloc}")
elif parts.path != expected:
problems.append(f"{old}: expected {expected}, got {parts.path or location}")
target_status, _ = head(expected)
if target_status != 200:
problems.append(f"{expected}: target returned {target_status}, not 200")
for line in problems:
print(line)
if problems:
sys.exit(1)
print(f"All {len(REDIRECTS)} redirects are correct.")
if __name__ == "__main__":
main()Change HOST to your domain before running it. A target that returns anything other than 200 reveals chains, since a second redirect would show up as 301. The script exits with a non-zero code on any problem, so you can run it in a deployment pipeline.
Keep the redirects in place
Redirects are not a temporary step. Follow these practices:
- Keep each 301 for at least a year. Google's guidance is that it can take a long time to transfer signals fully, and external links may keep pointing at the old URL for years. In practice, keep them indefinitely unless the redirect is costing you something.
- Do not reuse the old URL for unrelated content later. That breaks the signal you set up.
- Update internal links to point to the final URL, as covered in the internal-links chapter. A redirect should catch outside traffic, not your own navigation.
- Remove the old URLs from the XML sitemap and list the surviving URL, as covered in the sitemap chapter.
- Do not block old URLs in
robots.txt. A crawler that cannot fetch the old URL never sees the redirect.
Common failure modes
- Redirect loops.
/a/redirects to/b/and/b/redirects back to/a/, often through a trailing-slash rule. The browser shows "too many redirects" and crawlers give up. - Canonical and redirect disagree. The target's canonical tag points to a different URL, so search engines see conflicting instructions. The canonical on the target should refer to itself.
- Soft 404 from a bad target. A mass redirect to a category page or homepage. Search Console shows "Soft 404" for the old URLs.
- Fragments and parameters lost. Campaign parameters are dropped or tracking breaks. Decide whether to keep the query string and test it.
- Redirecting before the target is ready. If the merged page is not live yet, the old page's rankings are redirected into a 404.
After the redirects are live, the next step is to ask Google to recrawl, then measure results against your baseline.
Exercise
You merged three posts into /guides/seo-checklist/. A teammate redirects all three old URLs to the homepage with a 302 "to be safe". Name two problems with this, and give the fix.
Show answer
First, a 302 is a temporary redirect, so search engines may keep the old URLs indexed and may not pass their signals to the guide. Second, the homepage is not a close match for the old content, so Google may treat the redirects as soft 404s and discard the equity. The fix is to send each old URL to /guides/seo-checklist/ with a 301, directly in one hop, and then check that the guide returns 200 and does not redirect or carry a conflicting canonical.
Exercise
The verification script reports /blog/seo-tips-2019/: expected 301, got 200, although the nginx map contains the entry. Give two likely causes.
Show answer
Likely causes: the request was served by a different server block or a cache or CDN in front of nginx that does not have the rule; or the requested path differs from the map key, for example the key has a trailing slash but the request does not, or the map is exact-match on $uri and the real URL is different. Check which server block handles the host, confirm the key matches exactly, and reload nginx with nginx -s reload after fixing.