How it is calculated
A redirect is an answer from the server with a 3xx status code and a Location header. The browser, or a search engine, then asks for the new address instead. The generator writes the redirect as early as possible: in Nginx a server block that does nothing else, in Apache rewrite rules at the top of .htaccess.
# Apache, .htaccess
RewriteRule ^old-page\.html$ /new-page [R=301,L]
# Nginx
location = /old-page.html {
return 301 /new-page;
} Apache matches a regular expression, so dots and other special characters in a path are escaped, and the leading slash is left out because .htaccess removes it. Nginx compares an exact location as plain text. Both keep the visitor's query string unless the old address had one of its own. The HTTPS and www rules go to the final address in one step, rather than through two redirects in a row.
Example
Moving example.com to www.example.com: requests for http://example.com/blog, http://www.example.com/blog and https://example.com/blog all get one 301 to https://www.example.com/blog. Only https://www.example.com is served normally.
Which status code to use
| Code | Meaning | Use it for |
|---|---|---|
| 301 | Moved permanentlyA page or a site that moved for good. Search engines pass the old address's ranking to the new one. | A page or a site that moved for good. Search engines pass the old address's ranking to the new one. |
| 302 | Found, temporarily elsewhereA short detour, such as a sale page or maintenance. The old address stays in search results. | A short detour, such as a sale page or maintenance. The old address stays in search results. |
| 307 | Temporary, same methodLike 302, but a form sent with POST is sent again with POST. | Like 302, but a form sent with POST is sent again with POST. |
| 308 | Permanent, same methodLike 301, but keeps POST. Useful for an API or a form endpoint that moved. | Like 301, but keeps POST. Useful for an API or a form endpoint that moved. |
Questions
What is the difference between a 301 and a 302 redirect?
A 301 says the address moved for good: browsers remember it and search engines replace the old address with the new one. A 302 says the move is temporary, so the old address keeps its place. Use 301 for a new domain, HTTPS or a renamed page; Google treats a 302 kept for a long time like a 301 anyway.
Should I redirect www to non-www or the other way round?
Either works for search engines as long as you pick one and redirect the other to it. The short form is easier to type; www makes it simpler to set cookies only for the site and to point the name at a CDN with a CNAME record.
Why does my redirect loop?
Usually because HTTPS ends at a proxy or CDN and the server sees every request as http://, so it redirects even the https:// ones. Tick the proxy option to test the X-Forwarded-Proto header instead. In Cloudflare, the Full (strict) SSL mode avoids the problem. Clear the browser cache after fixing it: browsers keep 301 redirects.
Does Nginx read .htaccess files?
No. Nginx has no per-folder configuration files, so rules from an .htaccess file have to be written into the server's configuration, as in the Nginx tab here, and Nginx reloaded. That needs access to the server, which a VPS gives you and shared hosting usually does not.