Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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

location = / {
    rewrite ^ /something/else break;
}
share|improve this answer

This should do the job:

location / {
   rewrite  ^/$  /something/else  break;
}
share|improve this answer

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|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.