Sub topic 6
Changing a Slug Safely with 301 Redirects
This chapter assumes you can read basic HTML and nginx-style configuration, and that you know what an HTTP status code and a Location header are. It also assumes you know what a slug is and how to write a good one.
A published URL is a promise. Other sites link to it, readers bookmark it, and search engines have stored it with everything they know about the page. Changing the slug breaks that promise unless you redirect the old address to the new one. This chapter covers when to change a slug, how a 301 redirect carries signals across, and what usually goes wrong.
Why a slug change is risky
Search engines identify a page by its URL. If you rename /blog/seo-tips-2019 to /blog/internal-linking-tips and do nothing else, three things happen:
- The old URL returns a 404 error, so visitors and crawlers reach a dead end.
- Links from other sites that point to the old URL stop helping the page.
- The new URL starts as an unknown page with no history.
A 301 redirect closes this gap. It tells clients that the page has moved permanently and gives the new address.
When to change a slug at all
The best way to change a slug safely is to avoid needing to. Change one only when the benefit is clear:
- The slug is misleading or wrong, for example it names a product that no longer exists.
- The slug has a typo.
- The page's topic has changed enough that the old slug misdescribes it.
- You are merging duplicate pages into one.
Do not change slugs only to add a keyword, to remove a date from a page that still ranks, or to match a new naming style across old content. The gain is small, and the redirect adds risk and a small delay for every request.
How a 301 redirect works
A redirect is an HTTP response, not a page. The server answers the request for the old URL with a 3xx status code and a Location header. The client then requests the new URL.
The status code carries the meaning. The common choices are:
| Status | Meaning | Use it for |
|---|---|---|
| 301 | Moved permanently | A slug change that will not be undone |
| 308 | Permanent, request method preserved | Permanent moves where POST requests must stay POST |
| 302 | Found (temporary) | A short-lived move, such as a maintenance page |
| 307 | Temporary, request method preserved | A temporary move for non-GET requests |
For a slug change on an ordinary content page, use 301. Google treats 301 and 308 as permanent signals. A permanent redirect tells the search engine to replace the old URL with the new one in its index and to pass the old URL's signals to the new one.
A 302 says the move is temporary, so the search engine may keep the old URL indexed. If you use a 302 for a permanent move, the old URL can linger in results. Over time Google usually treats a long-lived 302 as permanent, but you should not rely on that.
Setting up the redirect
For a single page in nginx, the configuration is short:
server {
listen 80;
server_name example.com;
# Permanently redirect the old slug to the new one
location = /blog/seo-tips-2019 {
return 301 /blog/internal-linking-tips;
}
}The = in location = makes this an exact match, so only that one path is redirected and /blog/seo-tips-2019-extra is not caught by accident.
When you rename many pages, a lookup table is easier to maintain than one location block per page. This version uses nginx's map directive, which belongs in the http context:
map $uri $new_slug {
default "";
/blog/seo-tips-2019 /blog/internal-linking-tips;
/blog/old-shoe-guide /guides/running-shoes/;
}
server {
listen 80;
server_name example.com;
if ($new_slug) {
return 301 $new_slug;
}
}Requests whose path is not in the table get an empty value and pass through normally. A return inside if is a safe use of that directive. Other uses of if in nginx are known for surprising behavior.
If your site runs on a CMS, check first whether it already records old slugs and redirects them for you. Many do, but you should still verify the response code by testing.
The checklist for a slug change
Follow these steps in order. Skipping the later ones is the most common cause of lasting problems.
- Choose the new slug and confirm it follows your slug guidelines. Check that it does not already exist on your site.
- Publish the page at the new URL.
- Add a 301 redirect from the old URL to the new URL. Point it directly at the final destination.
- Update every internal link that pointed to the old URL so it points to the new one.
- Update the canonical tag on the page so it names the new URL.
- Update the XML sitemap to list the new URL and remove the old one.
- Test the old URL, check that it returns a 301, and check that the new URL returns 200.
- Keep the redirect in place for a long time. Google recommends at least a year.
Step 4 matters more than people expect. The redirect makes the old links work, but each redirected link costs an extra request and keeps the old URL in circulation. Links that point straight at the live URL are faster, and they show crawlers that you consider the new URL the real one.
Step 8 is also easy to get wrong. Other sites may keep linking to the old URL for years. If you remove the redirect after a few weeks, those links go dead again.
Common errors
Redirect chains
A chain happens when one redirect leads to another. Suppose /a redirects to /b, and later you rename /b to /c, adding a second redirect. A request for /a now takes two hops before it reaches the page.
Crawlers follow only a limited number of hops, and Google says it follows up to ten. Every hop also slows the visitor down. When you rename a page that is already the target of a redirect, edit the old redirect so that /a goes straight to /c.
Redirect loops
A loop happens when /a redirects to /b and /b redirects back to /a. The browser reports a "too many redirects" error, and crawlers give up. Loops usually come from a slug change that conflicts with an existing rule, such as a trailing-slash rule. Test every new redirect by requesting it.
Redirecting everything to the home page
When you delete a page, it is tempting to redirect it to the home page so nobody sees a 404. This is a mistake. A redirect should point to a page that closely replaces the old one. Search engines may treat a redirect to an unrelated page as a soft 404 and ignore it. If there is no good replacement, return a 404 or 410 status honestly.
Using a 302 by accident
Some frameworks and CMS plugins default to a 302 when you write a redirect. Always check the actual status code instead of assuming it.
Losing the query string or case variations
The nginx return 301 /new-path; rule above does not carry over the original query string. That is usually what you want for a content page. If the query string matters, for example for a search page, you need to append $is_args$args.
URL paths are case-sensitive on most servers. /Blog/Old-Post and /blog/old-post are different addresses. If old links exist in both forms, redirect both.
Forgetting protocol and host variants
If your site also answers on http://, or on www., make sure those variants reach the new slug with a single redirect, not one hop to fix the protocol and another for the slug. Combine the rules when you can.
Verifying the result
You can check a redirect with curl -I in a terminal, but a short script is better for checking many URLs and for spotting chains. This script uses only the standard library. It refuses to follow redirects automatically, so that you see each hop:
import urllib.error
import urllib.request
from urllib.parse import urljoin
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None # Do not follow; let the 3xx surface as an HTTPError
def trace(url, max_hops=10):
opener = urllib.request.build_opener(NoRedirect)
hops = []
for _ in range(max_hops):
try:
response = opener.open(url, timeout=10)
hops.append((url, response.status))
return hops
except urllib.error.HTTPError as err:
hops.append((url, err.code))
location = err.headers.get("Location")
if err.code in (301, 302, 303, 307, 308) and location:
url = urljoin(url, location)
else:
return hops
return hops
# Replace with your own URL. A healthy result is one 301 followed by one 200.
for hop in trace("https://example.com/blog/seo-tips-2019"):
print(hop)Read the output as a list of (url, status) pairs. A healthy slug change shows exactly two entries: the old URL with 301 and the new URL with 200. Three or more entries mean a chain. A repeated URL means a loop.
After the change, also watch Google Search Console. The old URL should drop out of the index and the new URL should appear. Rankings can move for a while after a migration and then settle. A page that does not recover after several weeks usually points to a mistake in the redirect or the canonical tag.
Summary
Change a slug only when the gain is real. Publish the new URL, redirect the old one with a single 301 straight to the final destination, and update internal links, canonical tags, and the sitemap. Then test the result and leave the redirect in place for at least a year.
Exercise
A page moved from /a to /b a year ago, with a 301 redirect. Today you rename it from /b to /c. What should you do with the old redirect, and why?
Show answer
Add a 301 from /b to /c, and change the existing redirect so /a also goes directly to /c. If you leave /a pointing at /b, requests for /a take two hops (/a to /b to /c), which is a redirect chain. It slows visitors and crawlers and risks losing signals. Also update internal links to /c.
Exercise
Your trace script prints [("https://example.com/old", 302), ("https://example.com/new", 200)]. What is wrong, and what do you change?
Show answer
The redirect uses a 302, which signals a temporary move, so search engines may keep the old URL indexed. Since the slug change is permanent, change the redirect to a 301 (or 308) and run the script again to confirm.