Redirect rule generator
Give the domain, which host name to keep and the old paths that moved: the page writes the Apache and nginx rules that send HTTP to HTTPS, the other host name to the one you keep and each old path to its new address. Give an address too, and it follows that address through the rules, one redirect at a time.
See the same redirects on the real site: curl asks for the headers only (-I) and follows each redirect (-L)
Apache: virtual hosts, the way Apache's documentation recommends
Apache: .htaccess, when you cannot change the virtual hosts
nginx: server blocks
Which status code
| Code | Name | Meaning |
|---|---|---|
301 | Moved Permanently | Permanent: from now on, references ought to use the new address. A browser may change a POST into a GET when it follows it. |
308 | Permanent Redirect | Permanent, as 301, and the browser must not change the method. Defined in 2014, so an old client may not know it. |
302 | Found | Temporary: clients ought to keep using the old address. A browser may change a POST into a GET. |
307 | Temporary Redirect | Temporary, as 302, and the browser must not change the method. |
A cache may keep a 301 or a 308 and reuse it without being told how long to, which it may not do with a 302 or a 307. So a wrong permanent redirect can stay in browsers after you fix it: try new rules with 302 or 307 first. nginx treats 308 as a redirect from version 1.13.0 on.
How the rules work
| Server | What the rules do |
|---|---|
| Apache | Redirect matches whole path segments from the start of the path: /service matches /service and /service/foo.txt but not /servicefoo.txt. What follows the matched part is added to the new address, and so is the query string; a new path without a host name stays on the same site. In one virtual host the first Redirect that matches wins, so put a longer path before a shorter one. For HTTP to HTTPS and for one host name to another, Apache's documentation prefers a Redirect in a virtual host of its own to mod_rewrite, as written here. |
| Apache .htaccess | Where the virtual hosts are out of reach, <If> (Apache 2.4) tests %{HTTPS}, which is on for HTTPS, and %{HTTP_HOST}. .htaccess files must be allowed <If> (AllowOverride All); Redirect alone needs AllowOverride FileInfo. Behind a load balancer or proxy that ends TLS, %{HTTPS} is always off, even for visitors on HTTPS, so the HTTPS rule would send them round without end. The X-Forwarded-Proto header can stand in only if you control that proxy and it replaces the header on every request. |
| nginx | A server block of its own with return and $request_uri (the path with its query string) is how nginx's documentation sends one host name to another. Each old path becomes a regular expression location: $1 is what follows the old path, and $is_args$args keeps the query string. nginx uses the first regular expression location that matches, in the order written, but a location with = for the same path, or a prefix location with ^~ that is the longest match, comes first. |
Each redirect is one more request for the browser. Here, an old path asked for over plain HTTP on the other host name takes two: one to HTTPS on the name you keep, one to the new path. curl follows up to 50 redirects before it gives up.
Sources
- Apache: the mod_alias reference (Redirect, its status values and its order of processing), the core reference (the If section), the expressions reference, and the How-To pages When not to use mod_rewrite and Redirecting and Remapping with mod_rewrite (Forcing HTTPS, Canonical Hostnames); 307 and 308 among the status lines of modules/
http/ http_protocol.c (httpd at commit 3b46369, branch 2.4.x), under the Apache License 2.0. - nginx: the ngx_http_rewrite_module reference (return), the ngx_http_core_module reference (location, listen and the variables) and Converting rewrite rules (nginx.org at commit 6e62624), under nginx's BSD-style licence.
- The status codes and caching: RFC 9110, HTTP Semantics, sections 15.1, 15.4, 15.4.2, 15.4.3, 15.4.8 and 15.4.9.
- curl: the manual pages of -I (--head), -L (--location) and --max-redirs (curl 8.23.0), under the curl licence.
More Web tools
-
SAML response decoder
A SAMLResponse or SAMLRequest decoded in your browser: issuer, status, NameID, audience, times and attributes.
-
Content-Security-Policy checker
What each directive of a policy allows, what falls back to default-src, the gaps, and the Apache and nginx lines.
-
TLS cipher suite lookup
0xC02F, its IANA name and its OpenSSL name, IANA's Recommended mark, and Windows' default order.
-
robots.txt tester
Paste a robots.txt and a path: which group a crawler follows, which rule wins, and why, by RFC 9309.