Redirect from www to sub-page


#1

Hi!

How can I redirect from www.example.com to www.example.com/new_location,
but do not redirect if there is already a full address such as
www.example.com/location?

I tried this

if ($host ~* “^www.example.com$”){
rewrite ^(.*)$ http://www.example.com/new_location break;
}

But of courese it creates an infinite loop.

Thanks,
H.


#2

On Thu, Jan 22, 2009 at 12:15:35PM +0100, ha we wrote:

But of courese it creates an infinite loop.
Never use
if ($host ~* “^www.example.com$”){

This means that you are on wrong way.
You should use

server {
    server_name  www.example.com;

    location = / {
         rewrite ^   http://www.example.com/new_location;
    }

    location / {
         ...
    }
}

#3

Hello!

On Thu, Jan 22, 2009 at 12:15:35PM +0100, ha we wrote:

Hi!

How can I redirect from www.example.com to www.example.com/new_location,
but do not redirect if there is already a full address such as
www.example.com/location?

location = / {
rewrite ^ http://www.example.com/new_location;
}

Maxim D.


#4

On Thu, Jan 22, 2009 at 06:23:24PM +0100, Marcin K. wrote:

    location = / {
         rewrite ^   http://www.example.com/new_location;
    }

Out of curiosity: does there exist any noticeable difference between
this and

rewrite ^/$ /new_location;

?

The

 rewrite ^/$ /new_location;

will run regex “^/$” for every request, while

 location = / {
     rewrite ^   http://www.example.com/new_location;

is just simple comparison with “/”. And the regex “^” will be run only
for “/” requests.


#5
 rewrite ^/$ /new_location;

will run regex “^/$” for every request, while

 location = / {
     rewrite ^   http://www.example.com/new_location;

is just simple comparison with “/”. And the regex “^” will be run only
for “/” requests.

Thanks for the explanation.

PS It could be interesting to measure how noticeable the difference
is. Precompiled hooked regexps aren’t that slow… But I don’t have
an idea of realistic benchmark.


#6
    location = / {
         rewrite ^   http://www.example.com/new_location;
    }

Out of curiosity: does there exist any noticeable difference between
this and

rewrite ^/$ /new_location;

?


#7

On Thu, Jan 22, 2009 at 09:59:43PM +0100, Marcin K. wrote:

Thanks for the explanation.

PS It could be interesting to measure how noticeable the difference
is. Precompiled hooked regexps aren’t that slow… But I don’t have
an idea of realistic benchmark.

Regexs may differ in times than simple comparison, however it will
not be noticeable in the big picture.

BTW, if you have

  location = / {
  }

  location / {
  }

then in 0.7.x “= /” is for free.