# Rewrite help

**URL:** <https://www.ruby-forum.com/t/rewrite-help/139001>\
**Category:** NGINX\
**Created:** [May 29, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/rewrite-help/139001 "2008-05-29T21:20:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [May 29, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/rewrite-help/139001/1 "2008-05-29T21:20:54Z")

</div>

How can I rewrite all:

/subscribe?foo=bar

into

/subscribe/?foo=bar

(note the trailing slash)

I’ve tried variations on this with no luck:

location / {  
root /var/www/docs/site.com/secure;  
index index.html index.htm index.php;  
rewrite ^/subscribe?(.\*) /subscribe/?$1 permanent;  
}

location /subscribe {  
index index.php;  
rewrite ^/subscribe?(.\*) /?$1 permanent;  
}

Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [May 29, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/rewrite-help/139001/2 "2008-05-29T21:33:51Z")

</div>

Also if it matters, this is a php URL being handled by fastcgi.

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [May 29, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/rewrite-help/139001/3 "2008-05-29T21:56:18Z")

</div>

Made some progress, but now the query\_string is being doubled.

location /subscribe {  
index index.php;  
rewrite ^/subscribe$ /subscribe/index.php?$query\_string permanent;  
}

The above just about does it, but I end up now going from  
/subscribe?foo=bar

to:  
/subscribe/index.php?foo=bar?foo=bar

If I don’t put in the $query\_string, then I get nothing at all.

Any help on what I’m doing wrong?

Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [May 29, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/rewrite-help/139001/4 "2008-05-29T22:35:14Z")

</div>

[SOLVED]

For posterity, here’s how I got it working:

I ended up adding this to my root location /

```
if ($request_filename !~* .*\.(jpg|jpeg|gif|css|png|js|ico)$) {
  rewrite ^/subscribe([^\/]*)$ /subscribe/index.php$1 permanent;
}

```

Seems to do the trick.  
-Jeff

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [May 6, 2010, 5:52am UTC](https://www.ruby-forum.com/t/rewrite-help/139001/5 "2010-05-06T05:52:46Z")

</div>

On Wed, May 05, 2010 at 10:36:29AM +0000, removed\_email\_address@domain.invalid wrote:

> Yes it should be rewrite to  
> [http://www.example.com/site/yahoo/test/article](http://www.example.com/site/yahoo/test/article)
> 
> Is there any way to do this?

```
     location / {
         proxy_pass http://www.example.com/site/yahoo/;
     }

```

What error do you see with this configuration ?

> > It works already on index. But when i accessing
> 
> > On Wed, May 05, 2010 at 03:22:36PM +0700, Glen L. wrote:
> > 
> > > However it’s not rewrited. It’s being redirected to  
> > > server\_name [google.example.com](http://google.example.com);  
> > > Subject: Re: rewrite help
> > > 
> > > > I have [www.example.com](http://www.example.com), [google.example.com](http://google.example.com), [yahoo.example.com](http://yahoo.example.com)
> > > 
> > > * * *
> > 
> > –  
> > nginx mailing list  
> > [nginx Info Page](http://nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> removed\_email\_address@domain.invalid  
> [nginx Info Page](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.  
[http://sysoev.ru/en/](http://sysoev.ru/en/)

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [May 6, 2010, 5:59am UTC](https://www.ruby-forum.com/t/rewrite-help/139001/6 "2010-05-06T05:59:48Z")

</div>

When I open /test/article, it’s redirected to index page again

Best Regards,

Glen L.

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [May 6, 2010, 10:55am UTC](https://www.ruby-forum.com/t/rewrite-help/139001/7 "2010-05-06T10:55:24Z")

</div>

On Wed, May 05, 2010 at 10:56:40AM +0000, removed\_email\_address@domain.invalid wrote:

> When I open /test/article, it’s redirected to index page again

The request may be proxied to nginx itself, to

server {  
listen 80;  
server\_name [www.example.com](http://www.example.com);

Try to set backedn address in  
proxy\_pass [http://www.example.com/site/yahoo/](http://www.example.com/site/yahoo/);  
instead of [www.example.com](http://www.example.com).

> On Wed, May 05, 2010 at 10:36:29AM +0000, removed\_email\_address@domain.invalid wrote:  
> What error do you see with this configuration ?
> 
> > > giving me bad gateway  
> > > is better than
> > > 
> > > > 
> > > 
> > > server {
> > > 
> > > > To: removed\_email\_address@domain.invalid
> > > > 
> > > > >
> > > 
> > > * * *
> > 
> > removed\_email\_address@domain.invalid
> 
> * * *
> 
> nginx mailing list  
> removed\_email\_address@domain.invalid  
> [nginx Info Page](http://nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> removed\_email\_address@domain.invalid  
> [nginx Info Page](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.  
[http://sysoev.ru/en/](http://sysoev.ru/en/)
