Nginx redirect rule has no affect

If the original URL is https://url.example.com or https://url.example.com/ then the normalized URI used by the rewrite and location directives will be /. The scheme, host name and query string have all been removed.

To perform a permanent redirect to a URL with a different host name:

Using rewrite (see this document for details):

rewrite ^/$ https://example.com/foo permanent;

Or using location and return (see this document for details):

location = / {
    return 301 https://example.com/foo;
}

The second solution is more efficient, as there are no regular expressions to process.

If the original URL includes a query string: The rewrite will append it automatically unless a trailing ? is added. The return will not, but can be added by appending $is_args$args.


If the scheme and host name are unchanged, then both statements can be simplified:

rewrite ^/$ /foo permanent;

Or:

location = / {
    return 301 /foo;
}