9

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?

4 Answers 4

7

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

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

This should do the job:

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

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;
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .