Agent Wranglers

WEB-85Recommended

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. Never redirect to an address taken from the request, as Redirect only to your own pages says. Other status codes follow Answer with the right status code.

Examples

HTTP example

HTTP/2 301
location: https://example.com/guides/new-name

nginx example

# 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.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/redirects-and-canonicals/redirect-codes">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.