Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Short Answer

Use 301 Moved Permanently when the old address will never come back: a domain move, the switch to HTTPS, a page with a new address. Use 302 Found when the old address stays the real one and the redirect is temporary: a maintenance page, a test, a redirect after login.

The Four Redirect Codes

CodeLastsRequest method
301PermanentMay change to GET
302TemporaryMay change to GET
307TemporaryKept
308PermanentKept

For historical reasons, browsers turn a POST into a GET when they follow a 301 or 302, so form data is lost. 307 and 308 were added to forbid that: the client repeats the same method with the same body at the new address. For ordinary page links, which are GET requests anyway, 301 and 308 behave the same.

How Browsers Treat Them

A browser may cache a 301 and go straight to the new address next time without asking the server. If you set a 301 by mistake and remove it, visitors who already saw it can keep being sent away until their cache expires or they clear it. A 302 is not cached unless the response says so. That is why it is safer to test a new redirect as 302 and change it to 301 when you are sure.

How Search Engines Treat Them

  • 301 and 308: the new address replaces the old one in search results, and links pointing to the old one count for the new one over time.
  • 302 and 307: the old address usually stays in search results, because it is expected to return.
  • Choose by intent: if a temporary redirect has become permanent, change it to 301 rather than leaving search engines to guess.

Which One to Use

SituationCode
New domain301
HTTP to HTTPS301 or 308
Page renamed301
Maintenance page302 or 307
Testing a redirect302
API endpoint moved308

Nginx Examples

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

location = /sale {
    return 302 https://example.com/maintenance.html;
}

Check the configuration with sudo nginx -t before reloading. A full HTTPS redirect is shown in Nginx redirect HTTP to HTTPS.

Apache Examples

Redirect 301 /old-page https://example.com/new-page
Redirect 302 /sale https://example.com/maintenance.html

A Redirect line without a code sends a 302, so always write the number. More patterns are in .htaccess redirect.

Check a Redirect with curl

curl -I http://example.com/old-page
curl -IL http://example.com/old-page

The first command shows the status and the Location header. -L follows every step, so you can see a chain such as HTTP to HTTPS to www. Keep chains to one step; loops end in ERR_TOO_MANY_REDIRECTS.

Common Redirect Mistakes

  • Redirecting to the home page: send each old address to its closest new page, not all of them to one place.
  • Relative or wrong targets: write the full new address, including https://, so the result does not depend on how the request arrived.
  • Losing the path: on a domain move, keep the rest of the address, as $request_uri does in Nginx.
  • Internal links left behind: update links on your own pages to the new address, so visitors do not pass through a redirect at all.

The 302 code on its own is covered in 302 Found.

Was this answer helpful?
Loading...