Workflow Tools Control Yard

Home / Web

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.

Which status code

CodeNameMeaning
301Moved PermanentlyPermanent: from now on, references ought to use the new address. A browser may change a POST into a GET when it follows it.
308Permanent RedirectPermanent, as 301, and the browser must not change the method. Defined in 2014, so an old client may not know it.
302FoundTemporary: clients ought to keep using the old address. A browser may change a POST into a GET.
307Temporary RedirectTemporary, 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

ServerWhat the rules do
ApacheRedirect 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 .htaccessWhere 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.
nginxA 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

More Web tools