# Rewrite help

**URL:** <https://www.ruby-forum.com/t/rewrite-help/188070>\
**Category:** NGINX\
**Created:** [May 5, 2010, 10:10am UTC](https://www.ruby-forum.com/t/rewrite-help/188070 "2010-05-05T10:10:13Z")\
**Posts on this page:** 12\
**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:** [May 5, 2010, 10:10am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/1 "2010-05-05T10:10:13Z")

</div>

Hi folks,

I need a rewrite rules like this

I have [www.example.com](http://www.example.com), [google.example.com](http://google.example.com), [yahoo.example.com](http://yahoo.example.com)

I need to rewrite

[Google.example.com](http://Google.example.com) Ã [www.example.com/site/google](http://www.example.com/site/google)

[Yahoo.example.com](http://Yahoo.example.com) Ã [www.example.com/site/yahoo](http://www.example.com/site/yahoo)

How can I do that?

---

<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:** [May 5, 2010, 10:17am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/2 "2010-05-05T10:17:15Z")

</div>

On Wed, May 5, 2010 at 3:09 PM, Glen L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

separate server block with rewrite on each blocks

–  
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:** [May 5, 2010, 10:26am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/3 "2010-05-05T10:26:01Z")

</div>

Have a rewrite rule in the location / for each of the three site  
configurations

Ray.

---

<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:** [May 5, 2010, 10:23am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/4 "2010-05-05T10:23:06Z")

</div>

I tried

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

```
    rewrite ^.+ http://www.example.com/site/yahoo$uri last;

```

}

However it’s not rewrited. It’s being redirected to  
[http://www.example.com/site/yahoo](http://www.example.com/site/yahoo)

---

<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:** [May 5, 2010, 10:34am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/5 "2010-05-05T10:34:13Z")

</div>

How about this?

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

```
   location / {
         rewrite ^(.*)$ http://www.example.com/site/yahoo$1 last;
   }

```

}

Ray.

---

<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:** [May 5, 2010, 11:06am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/6 "2010-05-05T11:06:25Z")

</div>

On Wed, May 05, 2010 at 03:22:36PM +0700, Glen L. wrote:

> I tried
> 
> server {  
> listen 80;  
> server\_name [yahoo.example.com](http://yahoo.example.com);
> 
> ```
> rewrite ^.+ http://www.example.com/site/yahoo$uri last;
> 
> ```
> 
> }
> 
> However it’s not rewrited. It’s being redirected to [http://www.example.com/site/yahoo](http://www.example.com/site/yahoo)

server {  
server\_name [yahoo.example.com](http://yahoo.example.com);  
location / {  
proxy\_pass [http://www.example.com/site/yahoo/](http://www.example.com/site/yahoo/);  
}  
}

server {  
server\_name [google.example.com](http://google.example.com);  
location / {  
proxy\_pass [http://www.example.com/site/google/](http://www.example.com/site/google/);  
}  
}

> > [Google.example.com](http://Google.example.com) à [www.example.com/site/google](http://www.example.com/site/google)
> 
> * * *
> 
> 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:** [May 5, 2010, 11:12am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/7 "2010-05-05T11:12:16Z")

</div>

The results is same as before, itâ€™s redirected to www

From: Ray [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Sent: 05 Mei 2010 15:33  
To: removed\_email\_address@domain.invalid  
Subject: Re: rewrite help

How about this?

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

```
   location / {

         rewrite ^(.*)$ http://www.example.com/site/yahoo$1 last;

   }

```

}

Ray.

On Wed, May 5, 2010 at 4:22 PM, Glen L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

I tried

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

```
   rewrite ^.+ http://www.example.com/site/yahoo$uri last;

```

}

However it’s not rewrited. It’s being redirected to  
[http://www.example.com/site/yahoo](http://www.example.com/site/yahoo)

---

<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:** [May 5, 2010, 11:31am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/8 "2010-05-05T11:31:00Z")

</div>

In that case, Igor’s configs should work for you.

Ray.

---

<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:** [May 5, 2010, 10:30am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/9 "2010-05-05T10:30:07Z")

</div>

On Wed, May 5, 2010 at 3:22 PM, Glen L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

well, if you actually want to access [www.ex.com/site/yahoo](http://www.ex.com/site/yahoo) without  
rewriting you have to configure each server blocks manually (root,  
etc).

–  
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:** [May 5, 2010, 11:56am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/10 "2010-05-05T11:56:46Z")

</div>

On Wed, May 05, 2010 at 04:50:01PM +0700, Glen L. wrote:

> Hi Igor,
> 
> It works already on index. But when i accessing  
> [yahoo.example.com/test/article](http://yahoo.example.com/test/article), it gives me error.

Where should it be proxied to ? To

[http://www.example.com/site/yahoo/test/article](http://www.example.com/site/yahoo/test/article)

?

> When i give proxy\_pass [http://www.example.com/site/yahoo.com$request\_uri](http://www.example.com/site/yahoo.com$request_uri), it  
> giving me bad gateway

This is because nginx try dynamically resolve [www.example.com](http://www.example.com) without  
resolver defined.

However, this way

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

```

is better than

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

```

> > server {  
> > server\_name [yahoo.example.com](http://yahoo.example.com);  
> > }
> > 
> > > I need to rewrite  
> > > separate server block with rewrite on each blocks
> 
> * * *
> 
> 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:** [May 5, 2010, 11:50am UTC](https://www.ruby-forum.com/t/rewrite-help/188070/11 "2010-05-05T11:50:55Z")

</div>

Hi Igor,

It works already on index. But when i accessing  
[yahoo.example.com/test/article](http://yahoo.example.com/test/article), it gives me error.

When i give proxy\_pass  
[http://www.example.com/site/yahoo.com$request\_uri](http://www.example.com/site/yahoo.com$request_uri), it  
giving me bad gateway

---

<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:** [May 5, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/rewrite-help/188070/12 "2010-05-05T12:37:44Z")

</div>

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?

Best Regards,

Glen L.
