# How to redirect to a query

**URL:** <https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248>\
**Category:** NGINX\
**Created:** [March 8, 2011, 4:18pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248 "2011-03-08T16:18:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 8, 2011, 4:18pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248/1 "2011-03-08T16:18:36Z")

</div>

I Need help with if someone enter [www.mysite.com](http://www.mysite.com) it redirect to  
[www.mysite.com/site.php?site=1](http://www.mysite.com/site.php?site=1)  
when I try it just loop and adds  
[www.mysite.com/site.php?site=1site.php?site=1](http://www.mysite.com/site.php?site=1site.php?site=1)  
site.php?site=1

till it fails

Posted at Nginx Forum:

> **[How to redirect to a query](https://forum.nginx.org/read.php?2,181278,181278#msg-181278)**
>
> I Need help with if someone enter www.mysite.com it redirect to www.mysite.com/site.php?site=1 when I try it just loop and adds www.mysite.com/site.php?site=1site.php?site=1 site.php?site=1 till it fails

---

<div class="post-metadata">

**Author:** ![sjark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@sjark](https://www.ruby-forum.com/u/sjark)\
**Post date:** [March 8, 2011, 10:05pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248/2 "2011-03-08T22:05:00Z")

</div>

On Tue, Mar 08, 2011 at 09:43:41AM -0500, sjark wrote:

Hi there,

> I Need help with if someone enter [www.mysite.com](http://www.mysite.com) it redirect to  
> [www.mysite.com/site.php?site=1](http://www.mysite.com/site.php?site=1)

location = / {  
rewrite ^ /site.php?site=1? permanent;  
}

in the right server{} block should do what you ask for.

It might do what you want if the initial request is for something like

> **[Website Hosting - Mysite.com](http://www.mysite.com/?query)**
>
> For affordable website hosting packages, go to MySite.com. You'll find complete and reliable website hosting with a range of prices and options.

Good luck,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sjark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@sjark](https://www.ruby-forum.com/u/sjark)\
**Post date:** [March 9, 2011, 10:52am UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248/3 "2011-03-09T10:52:36Z")

</div>

Same problem I get this error in chrome 310  
(net::ERR\_TOO\_MANY\_REDIRECTS):

```auto
server {
    listen 80 default;
    server_name www.mysite.com;
    root /home/www/mysite;
    include standard.conf; #php-fpm settings

    location = / {
      rewrite ^ /?index=site&id=1? permanent;
    }
}
```

Posted at Nginx Forum:

> **[Re: How to redirect to a query](https://forum.nginx.org/read.php?2,181278,181541#msg-181541)**
>
> I Need help with if someone enter www.mysite.com it redirect to www.mysite.com/site.php?site=1 when I try it just loop and adds www.mysite.com/site.php?site=1site.php?site=1 site.php?site=1 till it fails

---

<div class="post-metadata">

**Author:** ![sjark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@sjark](https://www.ruby-forum.com/u/sjark)\
**Post date:** [March 9, 2011, 10:57am UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248/4 "2011-03-09T10:57:37Z")

</div>

On 09.03.2011, at 12:51, sjark wrote:

> location = / {  
> rewrite ^ /?index=site&id=1? permanent;  
> }  
> }  
> [/code]

location = / {  
if ($arg\_index = “”) {  
rewrite ^ /?index=site&id=1 permanent;  
}

```
 fastcgi_pass ...
 ... etc ...

```

}

or you may do without redirect at all:

location = / {  
fastcgi\_pass …  
fastcgi\_param SCRIPT\_FILENAME /path/to/index.php;  
fastcgi\_param QUERY\_STRING index=site&id=1;  
… etc …  
}

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![sjark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@sjark](https://www.ruby-forum.com/u/sjark)\
**Post date:** [March 9, 2011, 11:52am UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248/5 "2011-03-09T11:52:14Z")

</div>

Thanks that works! one more question

can I do something like this if $host = ‘[one.mysite.com](http://one.mysite.com)’ and $arg\_index  
= “” then it should go to [one.mysite.com/?index=site&id=1](http://one.mysite.com/?index=site&id=1) if  
[two.mysite.com](http://two.mysite.com) it go to [A Virtual Services, Inc.](http://two.mysite.com/?index=site&id=2)

Tryed to replayed AND with & or && but it just give me a error  
“configuration file /etc/nginx/nginx.conf test failed”  
and I am not allowed todo a if inside of a if

```auto
location = / {
      if ($host = 'one.mysite.com' AND $arg_index = "") {rewrite ^
?index=site&id=1 permanent;}
      if ($host = 'two.mysite.com' AND $arg_index = "") {rewrite ^
?index=site&id=2 permanent;}
    }
```

Posted at Nginx Forum:

> **[Re: How to redirect to a query](https://forum.nginx.org/read.php?2,181278,181550#msg-181550)**
>
> I Need help with if someone enter www.mysite.com it redirect to www.mysite.com/site.php?site=1 when I try it just loop and adds www.mysite.com/site.php?site=1site.php?site=1 site.php?site=1 till it fails

---

<div class="post-metadata">

**Author:** ![sjark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@sjark](https://www.ruby-forum.com/u/sjark)\
**Post date:** [March 9, 2011, 1:03pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248/6 "2011-03-09T13:03:08Z")

</div>

On Wed, Mar 09, 2011 at 05:50:33AM -0500, sjark wrote:

Hi there,

> Thanks that works! one more question

The main docs are at [nginx](http://sysoev.ru/nginx/) with useful  
English-language  
content at [nginx documentation](http://www.nginx.org/en/docs/) and [http://wiki.nginx.org/](http://wiki.nginx.org/)

If you end up guessing the config syntax, could you mention which docs  
you read that were unclear? That way, hopefully, they can be fixed –  
or the “right” docs made more findable – so that the next person with  
the same difficulty will have an easier time. Thanks!

> can I do something like this if $host = ‘[one.mysite.com](http://one.mysite.com)’ and $arg\_index  
> = “” then it should go to [one.mysite.com/?index=site&id=1](http://one.mysite.com/?index=site&id=1) if  
> [two.mysite.com](http://two.mysite.com) it go to [A Virtual Services, Inc.](http://two.mysite.com/?index=site&id=2)

In nginx, “different host names” usually means “different server{}  
blocks”. That way, you only have one if() condition to worry about,  
which reduces it to a problem already solved 🙂

I realise this may be a simplified example, but: if “index” must always  
be present and always equal to “site”, then perhaps whatever handles  
the request could be told to assume that? This is similar to Igor’s  
“don’t redirect; handle it internally” suggestion.

Maybe similar analysis will make your full application configuration be  
clear too.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sjark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@sjark](https://www.ruby-forum.com/u/sjark)\
**Post date:** [March 9, 2011, 12:29pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248/7 "2011-03-09T12:29:54Z")

</div>

On Wed, Mar 09, 2011 at 04:51:31AM -0500, sjark wrote:

Hi there,

> Same problem I get this error in chrome 310  
> (net::ERR\_TOO\_MANY\_REDIRECTS):

> ```
> location = / {
> rewrite ^ /?index=site&id=1? permanent;
> 
> ```

That’s not the config that was suggested. The “?” immediately after the  
“/” makes all the difference.

The original question was about [www.mysite.com](http://www.mysite.com) redirecting to  
[www.mysite.com/site.php?site=1](http://www.mysite.com/site.php?site=1) (for which the suggested config should  
work).

This question is about [www.mysite.com](http://www.mysite.com) redirecting to  
[Website Hosting - Mysite.com](http://www.mysite.com/?index=site&id=1) (for which, as Igor showed, you must  
check the “args” of the query string).

And the next question is about [one.mysite.com](http://one.mysite.com) and [two.mysite.com](http://two.mysite.com) each  
redirecting to similar urls with different query strings.

I’ll reply to that one separately.

But different problems get different solutions. And if you don’t know  
whether the problems are different (and the docs give no hint), then  
it’s  
good to ask about the problem you care about rather than a simplified  
version that is possibly significantly different.

Cheers!

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sjark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@sjark](https://www.ruby-forum.com/u/sjark)\
**Post date:** [March 9, 2011, 1:36pm UTC](https://www.ruby-forum.com/t/how-to-redirect-to-a-query/204248/8 "2011-03-09T13:36:22Z")

</div>

Got everything working now. Was trying to get everything under one  
“server {” insted of split it up. Thanks for helping me out all 🙂

Posted at Nginx Forum:

> **[Re: How to redirect to a query](https://forum.nginx.org/read.php?2,181278,181561#msg-181561)**
>
> I Need help with if someone enter www.mysite.com it redirect to www.mysite.com/site.php?site=1 when I try it just loop and adds www.mysite.com/site.php?site=1site.php?site=1 site.php?site=1 till it fails
