# How could I permanently redirect dynamic url with querystrings?

**URL:** <https://www.ruby-forum.com/t/how-could-i-permanently-redirect-dynamic-url-with-querystrings/190413>\
**Category:** NGINX\
**Created:** [June 19, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/how-could-i-permanently-redirect-dynamic-url-with-querystrings/190413 "2010-06-19T15:54:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [June 19, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/how-could-i-permanently-redirect-dynamic-url-with-querystrings/190413/1 "2010-06-19T15:54:32Z")

</div>

Hi, all

I recently migrates my web server from lighty to nginx(nginx/0.7.67 +  
php-fpm), but there is something strange with the nginx rewrite rule.

The problem is, I wanna direct rules like this,

[http://www1.example.com/dispbbs.asp?boardID=15&ID=164752](http://www1.example.com/dispbbs.asp?boardID=15&ID=164752)

to a new domain(www2) and new format as follows,

[http://www2.example.com/thread-164752-1-1.html](http://www2.example.com/thread-164752-1-1.html)

However, I gets some strange results,

```auto
http://www2.example.com/thread-164752-1-1.html?boardID=15&ID=164752
```

Why? Why does the querystring ‘?boardID=15&ID=164752’ still  
appears and so strange?

Here is my settings in nginx.conf

```auto
...

#rewrite dynamic url with querystrings
if ($args ~* "boardid=[0-9]+&id=([0-9]+)") {
  set $tid $1;
  rewrite ^.*$ http://www2.example.com/thread-$tid-1-1.html permanent;
}
...
#here, i put some rewrite rules#
...
#at the end, rewrite undefined urls permanently
rewrite ^(.*) http://www2.example.com$1 permanent;
...
```

V/R,  
gavin

Posted at Nginx Forum:

> **[How could I permanently redirect dynamic url with querystrings?](https://forum.nginx.org/read.php?2,100268,100268#msg-100268)**
>
> Hi, all I recently migrates my web server from lighty to nginx(nginx/0.7.67 + php-fpm), but there is something strange with the nginx rewrite rule. The problem is, I wanna direct rules like this, http://www1.example.com/dispbbs.asp?boardID=15&

---

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [June 19, 2010, 4:29pm UTC](https://www.ruby-forum.com/t/how-could-i-permanently-redirect-dynamic-url-with-querystrings/190413/2 "2010-06-19T16:29:04Z")

</div>

On 19.06.2010 16:53, WisdomFusion wrote:

> #rewrite dynamic url with querystrings  
> if ($args ~\* “boardid=[0-9]+&id=([0-9]+)”) {  
> set $tid $1;  
> rewrite ^.\*$ [http://www2.example.com/thread-$tid-1-1.html](http://www2.example.com/thread-$tid-1-1.html) permanent;

- rewrite ^.\*$ [http://www2.example.com/thread-$tid-1-1.html](http://www2.example.com/thread-$tid-1-1.html) permanent;

- rewrite ^ [http://www2.example.com/thread-$tid-1-1.html](http://www2.example.com/thread-$tid-1-1.html)? permanent;

> #at the end, rewrite undefined urls permanently  
> rewrite ^(.\*) [http://www2.example.com$1](http://www2.example.com$1) permanent;

- rewrite ^(.\*) [http://www2.example.com$1](http://www2.example.com$1) permanent;

- rewrite ^ http://www2.example.com$request\_uri? permanent;

–  
Best regards,  
Gena

---

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [June 20, 2010, 1:06pm UTC](https://www.ruby-forum.com/t/how-could-i-permanently-redirect-dynamic-url-with-querystrings/190413/3 "2010-06-20T13:06:53Z")

</div>

## Gena M. Wrote:

> [http://www2.example.com/thread-$tid-1-1.html](http://www2.example.com/thread-$tid-1-1.html)  
> permanent;  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)  
> Thanks. ^^

It works perfect. Thanks a lot.

By the way, I always refers to configuration from  
[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpRewriteModule), but it’s so brief that  
some details like this problem I encountered are not included. Where  
could I find more detailed reference materials?

V/R,  
gavin

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,100268,100493#msg-100493](http://forum.nginx.org/read.php?2,100268,100493#msg-100493)

---

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [June 20, 2010, 2:39pm UTC](https://www.ruby-forum.com/t/how-could-i-permanently-redirect-dynamic-url-with-querystrings/190413/4 "2010-06-20T14:39:57Z")

</div>

On 20.06.2010 14:06, WisdomFusion wrote:

> > - rewrite ^.\*$ [http://www2.example.com/thread-$tid-1-1.html](http://www2.example.com/thread-$tid-1-1.html) permanent;
> > 
> > - rewrite ^ [http://www2.example.com/thread-$tid-1-1.html](http://www2.example.com/thread-$tid-1-1.html)? permanent;

> > - rewrite ^(.\*) [http://www2.example.com$1](http://www2.example.com$1) permanent;
> > 
> > - rewrite ^ http://www2.example.com$request\_uri? permanent;

> Thanks. ^^
> 
> It works perfect. Thanks a lot.
> 
> By the way, I always refers to configuration from  
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpRewriteModule), but it’s so brief that  
> some details like this problem I encountered are not included. Where  
> could I find more detailed reference materials?

high quality English documentation can be located  
at official nginx site: [nginx documentation](http://nginx.org/en/docs/)

information about “question mark as the last character”  
present at [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpRewriteModule:)

If in the line of replacement arguments are indicated, then the rest of  
the request arguments are appended to them. To avoid having them  
appended, place a question mark as the last character:

rewrite ^/users/(.\*)$ /show?user=$1? last;

‘^’ character is pcre regexp, it means ‘start of string’,  
more detailed manual about Perl Compatible Regular Expressions  
syntax is [perlre - Perl regular expressions - Perldoc Browser](http://perldoc.perl.org/perlre.html)  
or [http://www.pcre.org/pcre.txt](http://www.pcre.org/pcre.txt)

$request\_uri - internal nginx variable,  
it described at [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpCoreModule)

P.S.

quite often English documentation at [http://wiki.nginx.org/](http://wiki.nginx.org/)  
are more detailed than russian [nginx: документация](http://sysoev.ru/nginx/docs/)  
(wiki describes many undocumented nginx options and features)

but Russian documentation at [nginx: документация](http://sysoev.ru/nginx/docs/)  
always more accurate and correct than information at wiki,  
because Russian documentation maintained in sync  
with main development branch by creator of nginx.

it can be readed in english via Google Translate.

–  
Best regards,  
Gena

---

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [June 22, 2010, 3:59am UTC](https://www.ruby-forum.com/t/how-could-i-permanently-redirect-dynamic-url-with-querystrings/190413/5 "2010-06-22T03:59:04Z")

</div>

## Gena M. Wrote:

> permanent;  
> but it’s so brief that  
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpRewriteModule:)  
> ‘^’ character is pcre regexp, it means 'start of  
> P.S.  
> always more accurate and correct than information
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

It’s very kind of you, both official site and wiki are good references.  
Thanks for your paticient reply. 🙂

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,100268,101046#msg-101046](http://forum.nginx.org/read.php?2,100268,101046#msg-101046)
