Forum: NGINX redirect from www to sub-page

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Heiko W. (Guest)
on 2009-01-22 13:15
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.
Igor S. (Guest)
on 2009-01-22 13:29
(Received via mailing list)
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 / {
             ...
        }
    }
Maxim D. (Guest)
on 2009-01-22 13:35
(Received via mailing list)
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.
Marcin K. (Guest)
on 2009-01-22 20:43
(Received via mailing list)
>         location = / {
>              rewrite ^   http://www.example.com/new_location;
>         }

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

    rewrite ^/$ /new_location;

?
Igor S. (Guest)
on 2009-01-22 20:47
(Received via mailing list)
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.
Marcin K. (Guest)
on 2009-01-22 23:12
(Received via mailing list)
>      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.
Igor S. (Guest)
on 2009-01-23 11:55
(Received via mailing list)
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.
This topic is locked and can not be replied to.