# Rewrite problem

**URL:** <https://www.ruby-forum.com/t/rewrite-problem/222619>\
**Category:** NGINX\
**Created:** [September 4, 2012, 5:00am UTC](https://www.ruby-forum.com/t/rewrite-problem/222619 "2012-09-04T05:00:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 4, 2012, 5:00am UTC](https://www.ruby-forum.com/t/rewrite-problem/222619/1 "2012-09-04T05:00:14Z")

</div>

HI list  
I want to redirect  
[http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=](http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=)  
to  
[http://www.mysite.com/forum.php?mod=viewthread&tid=8123](http://www.mysite.com/forum.php?mod=viewthread&tid=8123)

This is what I place in the “server” block in the nginx.conf  
rewrite ^/bbs/viewthread.php?tid=([0-9]+).\*$  
/forum.php?mod=viewthread&tid=$1 last;

but when visit [http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=](http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=).  
It shows 404 error.

Could anyone point me out what is wrong?  
Thank you!

---

<div class="post-metadata">

**Author:** ![sosogh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sosogh](https://www.ruby-forum.com/u/sosogh)\
**Post date:** [September 4, 2012, 8:07am UTC](https://www.ruby-forum.com/t/rewrite-problem/222619/2 "2012-09-04T08:07:44Z")

</div>

sosogh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) a écrit :

HI list

I want to redirect  
[http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=](http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=)  
to  
[http://www.mysite.com/forum.php?mod=viewthread&tid=8123](http://www.mysite.com/forum.php?mod=viewthread&tid=8123)

This is what I place in the “server” block in the nginx.conf  
rewrite ^/bbs/viewthread.php?tid=([0-9]+).\*$  
/forum.php?mod=viewthread&tid=$1 last;

but when visit [http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=](http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=).  
It shows 404 error.

Could anyone point me out what is wrong?  
Thank you!

Take a look at your error log, it’s write which file nginx can’t find  
and you could see what’s wrong with your regex.  
Regards,

---

<div class="post-metadata">

**Author:** ![sosogh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sosogh](https://www.ruby-forum.com/u/sosogh)\
**Post date:** [September 4, 2012, 8:17am UTC](https://www.ruby-forum.com/t/rewrite-problem/222619/3 "2012-09-04T08:17:40Z")

</div>

Please adhere a tail of error log.

Thanks.

–

_Yours sincerely,_  
_Christopher M._

Ambassador/Contributor of Fedora Project and many others.

> **[Magic Transit LaB](http://cicku.me)**
>
> DDoS Protection for Networks - Cloudflare

---

<div class="post-metadata">

**Author:** ![sosogh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sosogh](https://www.ruby-forum.com/u/sosogh)\
**Post date:** [September 4, 2012, 9:58am UTC](https://www.ruby-forum.com/t/rewrite-problem/222619/4 "2012-09-04T09:58:48Z")

</div>

Hello!

On Tue, Sep 04, 2012 at 10:59:40AM +0800, sosogh wrote:

> It shows 404 error.
> 
> Could anyone point me out what is wrong?  
> Thank you!

The rewrite in question won’t match anything as rewrite work with  
URI without arguments. Use something like this instead:

location = /bbs/viewthread.php {  
if ($arg\_tid ~ \[1\]+$) {  
rewrite ^ /forum.php?mod=viewthread&tid=$arg\_tid? last;  
}

```
...

```

}

Some more details may be found here:

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#location)**
>
> This directive appeared in version 1.11.8.

> **[Module ngx\_http\_rewrite\_module](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite)**
>
> The ngx\_http\_rewrite\_module module is used to change request URI using PCRE regular expressions, return redirects, and conditionally select configurations.

> **[Module ngx\_http\_rewrite\_module](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if)**
>
> The ngx\_http\_rewrite\_module module is used to change request URI using PCRE regular expressions, return redirects, and conditionally select configurations.

Maxim D.

* * *

1. 0-9

---

<div class="post-metadata">

**Author:** ![sosogh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sosogh](https://www.ruby-forum.com/u/sosogh)\
**Post date:** [September 4, 2012, 2:07pm UTC](https://www.ruby-forum.com/t/rewrite-problem/222619/5 "2012-09-04T14:07:15Z")

</div>

Thank you very much .It works.  
But there is another problem.

I want  
[http://www.mysite.com/bbs/](http://www.mysite.com/bbs/)  
to

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

why the following conf does not work?  
rewrite ^/bbs/$ /index.php last;

I want  
[http://www.mysite.com/bbs/thread-4270-1-2.html](http://www.mysite.com/bbs/thread-4270-1-2.html)  
to  
[http://www.mysite.com/forum.php?mod=viewthread&tid=4270&extra=1&page=2](http://www.mysite.com/forum.php?mod=viewthread&tid=4270&extra=1&page=2)

why the following conf does not work?  
location ~\* ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ {  
rewrite ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+).html$  
/forum.php?mod=viewthread&tid=$1&extra=$2&page=$3 break;  
}

Thank you !

Hello!  
On Tue, Sep 04, 2012 at 10:59:40AM +0800, sosogh wrote:

> It shows 404 error.
> 
> Could anyone point me out what is wrong?  
> Thank you!  
> The rewrite in question won’t match anything as rewrite work with  
> URI without arguments. Use something like this instead:  
> location = /bbs/viewthread.php {  
> if ($arg\_tid ~ \[1\]+$) {  
> rewrite ^ /forum.php?mod=viewthread&tid=$arg\_tid? last;  
> }  
> …  
> }  
> Some more details may be found here:  
> [Module ngx\_http\_core\_module](http://nginx.org/r/location)  
> [Module ngx\_http\_rewrite\_module](http://nginx.org/r/rewrite)  
> [Module ngx\_http\_rewrite\_module](http://nginx.org/r/if)  
> Maxim D.

* * *

1. 0-9

---

<div class="post-metadata">

**Author:** ![sosogh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sosogh](https://www.ruby-forum.com/u/sosogh)\
**Post date:** [September 4, 2012, 7:21pm UTC](https://www.ruby-forum.com/t/rewrite-problem/222619/6 "2012-09-04T19:21:48Z")

</div>

Hello!

On Tue, Sep 04, 2012 at 08:05:46PM +0800, sosogh wrote:

> rewrite ^/bbs/$ /index.php last;  
> Define “does not work”. If in doubt, try looking into error log,  
> probably with

```
rewrite_log on;

```

added to your config (and error\_log set to at least “notice”  
level), it should give you enough information to find out what  
goes wrong.

> I want  
> [http://www.mysite.com/bbs/thread-4270-1-2.html](http://www.mysite.com/bbs/thread-4270-1-2.html)  
> to  
> [http://www.mysite.com/forum.php?mod=viewthread&tid=4270&extra=1&page=2](http://www.mysite.com/forum.php?mod=viewthread&tid=4270&extra=1&page=2)
> 
> why the following conf does not work?  
> location ~\* ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ {  
> rewrite ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+).html$  
> /forum.php?mod=viewthread&tid=$1&extra=$2&page=$3 break;  
> }

This will likely result in /forum.php source code being returned as  
you use “break” and doesn’t handle php in the location in  
question. See [Module ngx\_http\_rewrite\_module](http://nginx.org/r/rewrite) for details.

Maxim D.
