How to Set Up a 301 Redirect: .htaccess and Nginx Rules, 301 vs 302 vs 307 vs 308
Last updated: · 10 min read
When the address of a page changes, an HTTP redirect is how visitors and crawlers arriving at the old URL are sent to the new one: the server answers with a 3xx status code and a Location header, and the client follows it. A redirect done properly keeps links working through a site move, a change of URL structure or a switch to HTTPS. A rule done badly can cause an endless loop, needless chains or a 500 error across the whole site. This guide explains what the four status codes (301, 302, 307 and 308) mean, how mod_alias and mod_rewrite differ on Apache, what the Nginx equivalent looks like, and which mistakes are made most often. The rules in the worked example are real output from the Redirect Generator.
Formula
Apache mod_alias : Redirect <code> <old-path> <new-url>
Apache mod_rewrite : RewriteEngine On
RewriteRule ^/?<old-path>$ <new-url> [R=<code>,L]
Nginx : location = <old-path> { return <code> <new-url>; }
Code Meaning Request method (RFC 9110)
301 permanent POST may be changed to GET
302 temporary POST may be changed to GET
307 temporary method must not change
308 permanent method is preserved
Matching : Redirect matches a path prefix (sub-paths are carried along);
RewriteRule ^…$ and location = match the exact path only
Query : a query string such as ?id=5 is tested with RewriteCond %{QUERY_STRING} on Apache
and with $args on NginxAll three forms do the same job: they answer a request for the old path with the chosen status code and the new address. They differ in how they match. According to the Apache documentation, Redirect applies to any request beginning with the given path and appends the rest of the path to the target; only complete path segments match, so a rule for /service leaves /servicefoo.txt alone but does move /service/sub. RewriteRule takes a regular expression; the tool escapes special characters in the old path and anchors the expression between ^ and $ to make it an exact match. The leading /? is there for a reason: inside .htaccess the slash at the start of the path is stripped before comparison, and the Apache documentation notes that a pattern beginning with ^/ never matches in that context, whereas /? works in both. In Nginx, location = means an exact match, and the return directive accepts a URL with the codes 301, 302, 303, 307 and 308. The meaning of the status codes is defined in RFC 9110: with 301 and 302 a client is allowed, for historical reasons, to change a POST request into GET; 307 and 308 are their method-preserving counterparts.
What the inputs mean
- Rules
- One old path and one new address per line, separated by a space: "/old-page /new-page". The old path starts with /; the new address is either a path starting with / or a full URL starting with http(s)://. An address containing a space goes in double quotes. The old path may carry a query string such as ?id=5.
- Status code
- 301 (permanent), 302 (temporary), 307 (temporary, method preserved) or 308 (permanent, method preserved). Use 301 for permanent moves; choose 307 or 308 when POST requests, such as form submissions, must keep their method.
- Server
- Apache .htaccess (Redirect), Apache .htaccess (RewriteRule) or Nginx (location). The first matches by prefix and cannot test a query string; the other two match exactly.
- Extra rule
- None, Force HTTPS, www → non-www or non-www → www. If selected, a ready-made snippet is placed before your rules; for Nginx it is a separate server block in which example.com must be replaced with your own domain.
Worked example: Moving two pages: the same rules in three server formats
- Input: two lines, /old-page /new-page and /blog/old-post https://www.example.com/blog/new-post. Status code = 301, Server = Apache .htaccess (Redirect), Extra rule = None.
- The output is two lines: Redirect 301 /old-page /new-page and Redirect 301 /blog/old-post https://www.example.com/blog/new-post
- The tool's notes: 2 redirect rules written. Add the rules to the .htaccess file in the root of your site; mod_alias must be enabled. Redirect also carries sub-paths along: /old/sub goes to /new/sub. Choose mod_rewrite for exact matches only. Browsers cache permanent redirects (301, 308); it is safer to test with 302 first.
- Add a third line, /product.php?id=5 /products/blue-shirt. With Redirect selected, the tool refuses to generate the rules: Line 1: mod_alias (Redirect) cannot match a query string; choose mod_rewrite or Nginx.
- Switch the server to Apache .htaccess (RewriteRule). The output starts with RewriteEngine On, and the query-string rule is written first, together with a condition: RewriteCond %{QUERY_STRING} ^id=5$ and RewriteRule ^/?product\.php$ /products/blue-shirt [R=301,L,QSD]. The QSD flag stops the old ?id=5 query from being carried over to the new address.
- The other two rules become exact-match expressions: RewriteRule ^/?old-page$ /new-page [R=301,L] and RewriteRule ^/?blog/old-post$ https://www.example.com/blog/new-post [R=301,L]. Special characters such as the dot are escaped (product\.php).
- Switch the server to Nginx (location): location = /old-page { return 301 /new-page; } and location = /blog/old-post { return 301 https://www.example.com/blog/new-post; }. The query-string rule becomes a block, with if ($args = "id=5") { return 301 /products/blue-shirt; } inside location = /product.php {. Note: Add the rules inside the relevant server { … } block; test with nginx -t, then reload the configuration.
- Choosing status code 302 changes nothing but the code: Redirect 302 /old-page /new-page. The caching note shown for the permanent codes is absent from this output.
- Extra rule = Force HTTPS (with Redirect): the lines RewriteCond %{HTTPS} off and RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] are placed before your rules.
- Loop detection: entering /a /b together with /b /a gives Line 1: redirect loop. The old and new addresses are the same, or the rules point back to each other. With Redirect selected, the rule /blog /blog/archive raises the same error, because prefix matching would catch the new address too; the same input is accepted for RewriteRule: RewriteRule ^/?blog$ /blog/archive [R=301,L]
- If the same old path is entered twice: Line 2: this old path is already redirected on line 1.
Result: The same two rules come out as Redirect 301 /old-page /new-page and Redirect 301 /blog/old-post https://www.example.com/blog/new-post for mod_alias; as RewriteRule ^/?old-page$ /new-page [R=301,L] and RewriteRule ^/?blog/old-post$ https://www.example.com/blog/new-post [R=301,L] for mod_rewrite; and as location = /old-page { return 301 /new-page; } and location = /blog/old-post { return 301 https://www.example.com/blog/new-post; } for Nginx.
↪️ Redirect Generator (.htaccess/Nginx)
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
The code you choose depends on whether the move is permanent. According to Google's documentation on redirects, permanent redirects (301 and 308) are treated as a signal that the target URL should be the one shown in search results, while with temporary redirects (302, 303 and 307) the source page continues to be shown. The same page recommends a permanent server-side redirect whenever possible. Under RFC 9110, 301 and 308 responses are cacheable by default, so a wrong permanent redirect can linger in a visitor's browser after you delete the rule. That is why it is safer to try rules with 302 first and switch to 301 once they are confirmed. Avoid chains: instead of A → B → C, both A and B should point straight to C, because every hop is an extra request for the client. The tool produces rules as text; it does not connect to your server, cannot run the rules there and cannot know how they will interact with rules you already have. So take a backup of your .htaccess file before changing it, try the rules on a staging environment first, and then request each old URL and check the status code and the Location header (for example with curl -I). According to the Apache documentation, a syntax error or a directive that is not permitted in .htaccess produces a server error (HTTP 500), and that affects every page the file covers. On Nginx, run nginx -t before reloading the configuration.
Common mistakes
- Using 302 for a permanent move, or 301 for a temporary situation. A 301 is cached by browsers and hard to take back; a 302 on a permanent move gives search engines the wrong signal.
- Building redirect chains. Adding new rules on top of old ones creates A → B → C chains; point every old URL straight at the final one.
- Creating a loop. Rules such as /a → /b and /b → /a, or a Redirect that points into its own sub-path, end with a "too many redirects" error in the browser.
- Assuming Redirect matches exactly. Redirect 301 /blog /new also sends /blog/post-1 to /new/post-1; if you mean a single page, you need RewriteRule or RedirectMatch.
- Trying to match the query string in the Redirect or RewriteRule pattern. The pattern is compared with the path only; for the ?id=5 part, use RewriteCond %{QUERY_STRING}.
- Writing a RewriteRule pattern that starts with ^/ in .htaccess. The leading slash is stripped in that context, so the pattern never matches; use ^/? or ^ without the slash.
- Editing the live .htaccess without a backup. A single bad line can bring the whole site down with a 500 error; make sure you can restore the previous file.
- Moving a URL that receives form posts with 301 or 302. The client may turn the request into GET and the form data is lost; use 307 or 308 to preserve the method.
Frequently asked questions
Should I use 301 or 302?
Use 301 (or 308) when the address has changed for good, and 302 (or 307) when you intend to return to the old one. Google's documentation describes a permanent redirect as a signal to show the target URL in results, and a temporary one as leaving the source URL in results.
What is the difference between 301 and 308?
Both are permanent. Under RFC 9110, a client may change a POST request to GET after a 301, while 308 is the counterpart that preserves the method. For ordinary page URLs there is no practical difference, since those requests are GET anyway; for API endpoints and form targets 308 is the safer choice. The same relationship holds between 302 and 307.
I removed the rule. Why does my browser still redirect?
Permanent redirects (301 and 308) can be cached by the browser. Clear the browser cache or try a private window. To see what the server really returns, use a tool that does not cache, such as curl -I.
I added a rule to .htaccess and now the site returns a 500 error. Why?
According to the Apache documentation, a syntax error in .htaccess, or a directive the server configuration (AllowOverride) does not permit, produces a server error; the details are in the Apache error log. Restore your backup, then confirm with your hosting provider that mod_alias or mod_rewrite is enabled and that AllowOverride permits FileInfo directives.
Does Redirect carry the query string over to the new address?
Yes. According to the Apache documentation, the GET parameters of a request matched by Redirect are appended to the target URL. What it cannot do is choose a different target depending on the query string; for that, select RewriteRule or Nginx in the tool.
Should the HTTPS and www redirects be set up separately?
The tool lets you pick only one of them as the extra rule. If you set up both, take care not to create two-step chains, such as http://www going first to https://www and then to https://, and test all four starting addresses (http and https, with and without www) one by one.
Does the tool test my rules on a server?
No. It only generates text; it checks the format of each line, repeated old paths and loops among the rules. It cannot see how the rules interact with others on your server, which is why you should try them on a staging environment first.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.
Sources
- RFC 9110 — HTTP Semantics, section 15.4: Redirection 3xx
- Apache HTTP Server 2.4 — mod_alias, Redirect directive
- Apache HTTP Server 2.4 — mod_rewrite, RewriteRule directive
- Apache HTTP Server 2.4 — .htaccess files: troubleshooting
- nginx — ngx_http_rewrite_module, return directive
- nginx — ngx_http_core_module, location directive
- Google Search Central — Redirects and Google Search