Redirects and canonicals Required
Use the right redirect code
Use a server-side permanent redirect (301 or 308) for anything that moved for good. Use 302 or 307 only for short-term moves, and 303 only to show a result page after a form post.
How to do it
Permanent redirects tell search engines the new address is canonical. Temporary ones keep the old address in results. Use 308 or 307 when a form or API must keep its method and body. Browsers may turn a 301 or 302 POST into a GET. Redirect once. Fix trailing slash, case, index.html, www and http together, and keep the query string. Googlebot follows at most 10 hops, and every hop costs a round trip. Browsers may cache a 301 for good, so send Cache-Control: max-age=86400 on it until you are sure the target is right.
Example
HTTP/2 301
location: https://example.com/guides/new-name
# http and www go straight to the final host in one hop
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/ssl/example.com/fullchain.pem;
ssl_certificate_key /etc/ssl/example.com/privkey.pem;
return 301 https://example.com$request_uri;
}
How to check
curl -sI on an old address shows 301 or 308 and a location header with the final address. curl -sIL shows at most one redirect before a 200.
For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers website playbook: https://agentwranglers.se/playbook/website/redirects-and-canonicals/redirect-codes.