# How to rewrite these

**URL:** <https://www.ruby-forum.com/t/how-to-rewrite-these/183732>\
**Category:** NGINX\
**Created:** [February 19, 2010, 9:11am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732 "2010-02-19T09:11:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 9:11am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/1 "2010-02-19T09:11:42Z")

</div>

Hi Nginx,

I’m migrating my website into onother application. I need to rewrite  
some of  
these

[http://www.mydomain.com/category/subcategory](http://www.mydomain.com/category/subcategory)

into

> **[Web Hosting, Domain Name Registration - MyDomain.com](https://www.mydomain.com/)**
>
> Small business web hosting offering additional business services such as: domain name registrations, email accounts, web services, and various small business solutions.

[http://www.mydomain.com/application/category/subcategory](http://www.mydomain.com/application/category/subcategory)  
application/category/subcategory

I need to insert /application before my category name

Thank’s before

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 10:10am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/2 "2010-02-19T10:10:50Z")

</div>

On Fri, Feb 19, 2010 at 03:11:09PM +0700, Glen L. wrote:

> [http://www.mydomain.com/application/category/subcategory](http://www.mydomain.com/application/category/subcategory)
> 
> I need to insert /application before my category name

If you use proxying, then:

```
location / {
    proxy_pass http://backend/application/;
}

```

–  
Igor S.

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

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 11:03am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/3 "2010-02-19T11:03:31Z")

</div>

No i’m not proxying…

Because all I need is to rewrite arround 50 that category

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 11:06am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/4 "2010-02-19T11:06:26Z")

</div>

On Fri, Feb 19, 2010 at 05:02:44PM +0700, Glen L. wrote:

> No i’m not proxying..

What do you use ?

> Because all I need is to rewrite arround 50 that category

Could you show some examples ?

> > I’m migrating my website into onother application. I need to rewrite some  
> > I need to insert /application before my category name  
> > [Igor Sysoev](http://sysoev.ru/en/)  
> > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)  
> > –  
> > Igor S.  
> > [Igor Sysoev](http://sysoev.ru/en/)

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 11:46am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/5 "2010-02-19T11:46:15Z")

</div>

Example

Old : [http://www.mydomain.com/breaking-news](http://www.mydomain.com/breaking-news)  
New : [http://www.mydomain.com/category/breaking-news](http://www.mydomain.com/category/breaking-news)

Old : [http://www.mydomain.com/national](http://www.mydomain.com/national)  
New : [http://www.mydomain.com/category/national](http://www.mydomain.com/category/national)

Old : [http://www.mydomain.com/international](http://www.mydomain.com/international)  
New : [http://www.mydomain.com/category/international](http://www.mydomain.com/category/international)

I’m using php-fpm for these

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 11:51am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/6 "2010-02-19T11:51:31Z")

</div>

Is that posibble to use somethink like

```
 location ~ ^/breaking-news|national|...) {
rewrite ^ /category$request_uri
 }

```

?

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 11:47am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/7 "2010-02-19T11:47:25Z")

</div>

On Fri, Feb 19, 2010 at 05:11:14PM +0700, Glen L. wrote:

> I’m using php-fpm for these

Something like this:

```
 location ~ ^/breaking-news|national|...) {
     fastcgi_pass ...
     fastcgi_parm SCRIPT_FILENAME /path/to/scripts/category$uri;
     ...
 }

```

> What do you use ?
> 
> > > 
> > 
> > ```
> > proxy_pass http://backend/application/;
> > 
> > ```
> > 
> > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

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

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 11:51am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/8 "2010-02-19T11:51:31Z")

</div>

On Fri, Feb 19, 2010 at 05:20:20PM +0700, Glen L. wrote:

> Is that posibble to use somethink like
> 
> ```
> location ~ ^/breaking-news|national|...) {
> rewrite ^ /category$request_uri      
> 
> ```

- 

```
rewrite ^ /category$request_uri;

```

- rewrite ^ /category$uri;

> ```
> }
> 
> ```
> 
> ?

Possible, but why to use rewrite, if you may pass request directly where  
you want ? In my opinion rewrites cause spaghetti configuration.

> > Old : [http://www.mydomain.com/breaking-news](http://www.mydomain.com/breaking-news)  
> > Something like this:  
> > To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > Could you show some examples ?
> > 
> > > > [http://www.mydomain.com/application/category/subcategory](http://www.mydomain.com/application/category/subcategory)  
> > > > –  
> > > > nginx mailing list  
> > > > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

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

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 12:04pm UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/9 "2010-02-19T12:04:59Z")

</div>

I prefer to rewrite it, because what we concerned is google indexing…  
sometimes google will remove the old address, and getting the new one  
So that when people accessing our site, they will bookmark the new  
address

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 7:20am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/10 "2010-02-21T07:20:55Z")

</div>

Bump…

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 7:37am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/11 "2010-02-21T07:37:19Z")

</div>

On Sat, Feb 20, 2010 at 8:17 PM, Glen L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Bump..

you should start learning writing rewrite rules…

rewrite ^/news/(.+)/title  
[http://app.mydomain.com/redirect/application/$1](http://app.mydomain.com/redirect/application/$1) permanent;

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 19, 2010, 12:43pm UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/12 "2010-02-19T12:43:37Z")

</div>

OK thank’s for ur suggestion… It’s working like a charm… 1 more  
question

I have a url [http://www.mydomain.com/news/$id/title](http://www.mydomain.com/news/$id/title)

How can I redirect it to  
[http://app.mydomain.com/redirect/application/$id](http://app.mydomain.com/redirect/application/$id) ?

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 7:58am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/13 "2010-02-21T07:58:23Z")

</div>

On Sat, Feb 20, 2010 at 8:45 PM, Glen L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is that posibble to detect if (.+) is numbers only?

yes.

the method should be mentioned in any guide about regular expressions.  
Which includes this [1] and this [2].

[1] [http://www.regextester.com/pregsyntax.html](http://www.regextester.com/pregsyntax.html)

[2] [Regular Expressions Reference](http://www.regular-expressions.info/reference.html)

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 7:48am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/14 "2010-02-21T07:48:26Z")

</div>

Is that posibble to detect if (.+) is numbers only?

So if [http://www.mydomain.com/news/1231245/title](http://www.mydomain.com/news/1231245/title)  
Then rewrite to [http://app.mydomain.com/redirect/application/1231245](http://app.mydomain.com/redirect/application/1231245)

If [http://www.mydomain.com/news/category/title](http://www.mydomain.com/news/category/title)  
Then do nothing… just bypass to the content

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 8:08am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/15 "2010-02-21T08:08:55Z")

</div>

Thank’s for your prompt reply…

However i tried this

rewrite “/news/([0-9] {2})([0-9] {2})([0-9] {2})/$”  
[http://app.mydomain.com/redirect/application/$1$2$3](http://app.mydomain.com/redirect/application/$1$2$3);

but it doesn’t work. What I’ve missed?

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 8:39am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/16 "2010-02-21T08:39:22Z")

</div>

the spaces: “] {” -\> “]{”.

may i ask why you catch three times two numbers instead of one time six?

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 8:44am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/17 "2010-02-21T08:44:38Z")

</div>

Glen L. a écrit :

> Thank’s for your prompt reply..
> 
> However i tried this
> 
> rewrite “/news/([0-9] {2})([0-9] {2})([0-9] {2})/$”  
> [http://app.mydomain.com/redirect/application/$1$2$3](http://app.mydomain.com/redirect/application/$1$2$3);
> 
> but it doesn’t work. What I’ve missed?

The regexp pattern isn’t correct. You shouldn’t have spaces before  
the “{2}” unless you really want to match two consecutive spaces.

Even without those spaces, this pattern would match urls like  
“/news/123456/” (without “/title” and with a trailing slash), while  
you said you need to match “/news/$id/title” instead. Also, if you  
want to fade out the old url in the long run, you could use a  
permanent redirect.

You probably want something like that:  
rewrite ^/news/([0-9]{6})/title$  
[http://app.mydomain.com/redirect/application/$1](http://app.mydomain.com/redirect/application/$1) permanent;

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 8:43am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/18 "2010-02-21T08:43:27Z")

</div>

The spaces fixed the problem 🙂

By the way, if i have only 1, 2, 3, 4, 5 digits of numbers, it’s not  
redirected

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 8:48am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/19 "2010-02-21T08:48:09Z")

</div>

honestly, i think you should really go through some regex tutorials

---

<div class="post-metadata">

**Author:** ![Glen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@Glen\_L](https://www.ruby-forum.com/u/Glen_L)\
**Post date:** [February 21, 2010, 8:58am UTC](https://www.ruby-forum.com/t/how-to-rewrite-these/183732/20 "2010-02-21T08:58:40Z")

</div>

It means that if I have 6 digits of numbers, it will redirect to the url  
i’ve described

But if I have 5 digits of numbers, it’s not redirecting to the URL.

[Next page](https://www.ruby-forum.com/t/how-to-rewrite-these/183732.md?page=2)
