nginx rewrite base url

27,996

Solution 1

You really want to be matching exactly the root URL in your location block, not "absolutely everything":

location = / {
    rewrite ^ /something/else break;
}

Solution 2

This should do the job:

location / {
   rewrite  ^/$  /something/else  break;
}

Solution 3

Or you can do this :

location = / {
     rewrite ^/$    http://example.com/an/other/path;
}

Nginx sends a HTTP 302 redirect to the client automatically when the rewriten URL starts with the 'http' scheme.

Solution 4

The rewrite statement performs an internal rewrite by default unless the target is an absolute URL or you set the redirect or permanent flags. Something like this would return an HTTP redirect to the client:

rewrite ^/$ /something/else redirect;
Share:
27,996

Related videos on Youtube

Slepov Alexander
Author by

Slepov Alexander

Updated on September 18, 2022

Comments

  • Slepov Alexander
    Slepov Alexander over 1 year

    I would like the root url

    http://www.example.com
    

    to redirect to

    http://www.example.com/something/else
    

    This is because some weird WP plugin always sets a cookie on the base url, which doesn't let me cache it.

    I tried this directive:

    location / {
        rewrite  ^  /something/else  break;
    }
    

    But 1) there is no redirect and 2) pages start shooting more than 1,000 requests to my server. With this one:

     location / {
            rewrite  ^  http://www.example.com/something/else  break;
        }
    

    Chrome reports a redirect loop.

    What's the correct regexp to use?