# Rewrite scheme for url redirect

**URL:** <https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323>\
**Category:** NGINX\
**Created:** [January 26, 2011, 10:12pm UTC](https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323 "2011-01-26T22:12:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 26, 2011, 10:12pm UTC](https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323/1 "2011-01-26T22:12:58Z")

</div>

I’m going crazy with this since I can’t get it working in any way…

I have a subdomain on a server, let’s call it [images.site1.com](http://images.site1.com),  
dedicated to serve images. Due to some bandwidth limitation i got a more  
capable server so the webpages will remain on [site1.com](http://site1.com) but the images  
will be moved to [images.site2.com](http://images.site2.com)

Since I had to change the names of images folders on the new server now  
I need to rewrite the urls in order to fit my needs. Only the webserver  
on [site1.com](http://site1.com) runs NginX, so i need to do rewrite the original ruls  
following a regular expression (regex) and do something like:

[http://images.site1.com/oldfolderjpg/123456\_1.jpg](http://images.site1.com/oldfolderjpg/123456_1.jpg)  
–rewrite–\>  
[http://images.site2.com/newfolderjpg/12/34/new\_images/123456/123456\_image\_1\_large.jpg](http://images.site2.com/newfolderjpg/12/34/new_images/123456/123456_image_1_large.jpg)

- The first and most logic rewrite scheme I tought about is:

rewrite ^/oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)  
permanent;But it didn’t work (I really don’t know why)

- The second way I tried out is:

location /oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$ {  
rewrite ^  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)  
permanent;  
}And it didn’t work either…

I tried this config too:

location ~ oldfolder/ {  
rewrite ^([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)  
permanent;  
}But with no luck…

After that I tried an old config cheme i used for another site (on the  
same box) for a cdn config:

location ~ /oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])_([0-9]).jpg  
{  
rewrite ^/(.\*)$  
$scheme://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image_$4\_large.jpg;  
}I’ve been triying in several ways but nginx keeps ignoring my rewrite  
scheme redirecting to my default error image located at  
[images.site1.com/error.jpg](http://images.site1.com/error.jpg)

Can anyone help me with this?

I’ll write down my subdomain server config for your facility:

## [images.site1.com](http://images.site1.com)

```
server {
    listen 80;
    server_name images.site1.com;
    error_page 404 http://images.site1.com/error.jpg;
    root /home/sites/site1.com/images;
    }It's a very basic server config, that's why I can't understand

```

why nginx doesn’t rewrite the images following such scheme…

Posted at Nginx Forum:

> **[rewrite scheme for url redirect](https://forum.nginx.org/read.php?2,169853,169853#msg-169853)**
>
> I'm going crazy with this since I can't get it working in any way... I have a subdomain on a server, let's call it images.site1.com, dedicated to serve images. Due to some bandwidth limitation i got a more capable server so the webpages will remain...

---

<div class="post-metadata">

**Author:** ![sevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@sevy](https://www.ruby-forum.com/u/sevy)\
**Post date:** [January 26, 2011, 10:40pm UTC](https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323/2 "2011-01-26T22:40:36Z")

</div>

Hi Sevy,

Not an nginx expert, but you say :

“[http://images.site1.com/oldfolderjpg/123456\_1.jpg](http://images.site1.com/oldfolderjpg/123456_1.jpg)”  
and  
“rewrite ^/oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$”

Shouldn’t the rewrite be :  
rewrite ^/oldfolderjpg/([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$

Notice the oldfolderjpg instead of oldfolder. That would explain a  
lot…

Antoine.

----- Original Message ----  
From: sevy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wed, January 26, 2011 10:11:30 PM  
Subject: rewrite scheme for url redirect

I’m going crazy with this since I can’t get it working in any way…

I have a subdomain on a server, let’s call it [images.site1.com](http://images.site1.com),  
dedicated to serve images. Due to some bandwidth limitation i got a more  
capable server so the webpages will remain on [site1.com](http://site1.com) but the images  
will be moved to [images.site2.com](http://images.site2.com)

Since I had to change the names of images folders on the new server now  
I need to rewrite the urls in order to fit my needs. Only the webserver  
on [site1.com](http://site1.com) runs NginX, so i need to do rewrite the original ruls  
following a regular expression (regex) and do something like:

[http://images.site1.com/oldfolderjpg/123456\_1.jpg](http://images.site1.com/oldfolderjpg/123456_1.jpg)  
–rewrite–\>  
[http://images.site2.com/newfolderjpg/12/34/new\_images/123456/123456\_image\_1\_large.jpg](http://images.site2.com/newfolderjpg/12/34/new_images/123456/123456_image_1_large.jpg)

- The first and most logic rewrite scheme I tought about is:

rewrite ^/oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)

permanent;But it didn’t work (I really don’t know why)

- The second way I tried out is:

location /oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$ {  
rewrite ^  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)

permanent;  
}And it didn’t work either…

I tried this config too:

location ~ oldfolder/ {  
rewrite ^([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)

permanent;  
}But with no luck…

After that I tried an old config cheme i used for another site (on the  
same box) for a cdn config:

location ~ /oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])_([0-9]).jpg  
{  
rewrite ^/(.\*)$  
$scheme://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image_$4\_large.jpg;

}I’ve been triying in several ways but nginx keeps ignoring my rewrite  
scheme redirecting to my default error image located at  
[images.site1.com/error.jpg](http://images.site1.com/error.jpg)

Can anyone help me with this?

I’ll write down my subdomain server config for your facility:

## [images.site1.com](http://images.site1.com)

```
server {
    listen 80;
    server_name images.site1.com;
    error_page 404 http://images.site1.com/error.jpg;
    root /home/sites/site1.com/images;
    }It's a very basic server config, that's why I can't understand

```

why nginx doesn’t rewrite the images following such scheme…

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

* * *

nginx mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> **[mailman.nginx.org archive](http://mailman.nginx.org/pipermail/)**

---

<div class="post-metadata">

**Author:** ![sevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@sevy](https://www.ruby-forum.com/u/sevy)\
**Post date:** [January 26, 2011, 10:48pm UTC](https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323/3 "2011-01-26T22:48:07Z")

</div>

That was my typeing error cause I was writing this post on a text file  
before posting it.  
I just meant /oldfolder/ so forget the jpg at the end of the folder  
name.

Posted at Nginx Forum:

> **[Re: rewrite scheme for url redirect](https://forum.nginx.org/read.php?2,169853,169871#msg-169871)**
>
> I'm going crazy with this since I can't get it working in any way... I have a subdomain on a server, let's call it images.site1.com, dedicated to serve images. Due to some bandwidth limitation i got a more capable server so the webpages will remain...

---

<div class="post-metadata">

**Author:** ![sevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@sevy](https://www.ruby-forum.com/u/sevy)\
**Post date:** [January 27, 2011, 10:29am UTC](https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323/4 "2011-01-27T10:29:14Z")

</div>

Hi Sevy,

I looked at your rewrite rule, created a similar setup (with nginx  
0.8.53)  
and…the rewrite rule works as expected. I attach my setup (the conf  
file and  
the whole directory in tar.bz2).  
The only thing I can think of at this point is that your NGINX was  
compiled  
without regex support (PCRE).

Feel free to send me your conf file.

Antoine.

----- Original Message ----  
From: sevy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wed, January 26, 2011 10:11:30 PM  
Subject: rewrite scheme for url redirect

I’m going crazy with this since I can’t get it working in any way…

I have a subdomain on a server, let’s call it [images.site1.com](http://images.site1.com),  
dedicated to serve images. Due to some bandwidth limitation i got a more  
capable server so the webpages will remain on [site1.com](http://site1.com) but the images  
will be moved to [images.site2.com](http://images.site2.com)

Since I had to change the names of images folders on the new server now  
I need to rewrite the urls in order to fit my needs. Only the webserver  
on [site1.com](http://site1.com) runs NginX, so i need to do rewrite the original ruls  
following a regular expression (regex) and do something like:

[http://images.site1.com/oldfolderjpg/123456\_1.jpg](http://images.site1.com/oldfolderjpg/123456_1.jpg)  
–rewrite–\>  
[http://images.site2.com/newfolderjpg/12/34/new\_images/123456/123456\_image\_1\_large.jpg](http://images.site2.com/newfolderjpg/12/34/new_images/123456/123456_image_1_large.jpg)

- The first and most logic rewrite scheme I tought about is:

rewrite ^/oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)

permanent;But it didn’t work (I really don’t know why)

- The second way I tried out is:

location /oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$ {  
rewrite ^  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)

permanent;  
}And it didn’t work either…

I tried this config too:

location ~ oldfolder/ {  
rewrite ^([0-9][0-9])([0-9][0-9])([0-9][0-9])\_([0-9]).jpg$  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)

permanent;  
}But with no luck…

After that I tried an old config cheme i used for another site (on the  
same box) for a cdn config:

location ~ /oldfolder/([0-9][0-9])([0-9][0-9])([0-9][0-9])_([0-9]).jpg  
{  
rewrite ^/(.\*)$  
$scheme://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image_$4\_large.jpg;

}I’ve been triying in several ways but nginx keeps ignoring my rewrite  
scheme redirecting to my default error image located at  
[images.site1.com/error.jpg](http://images.site1.com/error.jpg)

Can anyone help me with this?

I’ll write down my subdomain server config for your facility:

## [images.site1.com](http://images.site1.com)

```
server {
    listen 80;
    server_name images.site1.com;
    error_page 404 http://images.site1.com/error.jpg;
    root /home/sites/site1.com/images;
    }It's a very basic server config, that's why I can't understand

```

why nginx doesn’t rewrite the images following such scheme…

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

---

<div class="post-metadata">

**Author:** ![sevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@sevy](https://www.ruby-forum.com/u/sevy)\
**Post date:** [January 26, 2011, 11:15pm UTC](https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323/5 "2011-01-26T23:15:07Z")

</div>

On 26 Jan 2011 21h11 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> original ruls following a regular expression (regex) and do  
> permanent  
> On [images.site1.com](http://images.site1.com) config try this:

location /oldfolder {  
rewrite ^/oldfolder/(\d{2})(\d{2})(\d{2})\_(\d).jpg$  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)?  
permanent;  
}

— appa

---

<div class="post-metadata">

**Author:** ![sevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@sevy](https://www.ruby-forum.com/u/sevy)\
**Post date:** [January 27, 2011, 3:33pm UTC](https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323/6 "2011-01-27T15:33:23Z")

</div>

On 27 Jan 2011 14h17 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Because the regexp you posted has curly braces. So, following nginx  
> wiki ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule#rewrite)) i tried to  
> wrap it into both single and double quotes but with no result:

I overlooked that ☹

> Nginx doesn’t redirect my uri requests, so the problem still  
> remains…

I suggest you check the logs and/pr enable the debug flag and trace  
Nginx request processing. It will tell what’s really going on. The  
above regex does capture the patterns you want, so something else must  
be creating the problems.

— appa

---

<div class="post-metadata">

**Author:** ![sevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@sevy](https://www.ruby-forum.com/u/sevy)\
**Post date:** [January 27, 2011, 3:18pm UTC](https://www.ruby-forum.com/t/rewrite-scheme-for-url-redirect/202323/7 "2011-01-27T15:18:54Z")

</div>

## António P. P. Almeida Wrote:

> > more capable server so the webpages will remain  
> > (regex) and do  
> > tought about is:  
> > On [images.site1.com](http://images.site1.com) config try this:
> 
> location /oldfolder {  
> rewrite  
> ^/oldfolder/(\d{2})(\d{2})(\d{2})_(\d).jpg$  
> [http://images.site2.com/newfolder/$1/$2/new\_images](http://images.site2.com/newfolder/$1/$2/new_images)  
> /$1$2$3/$1$2$3\_image_$4\_large.jpg? permanent;  
> }

I tried your solution but it has a semantic error cause nginx replies

nginx: [emerg] directive “rewrite” is not terminated by “;”

Because the regexp you posted has curly braces. So, following nginx wiki  
([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule#rewrite)) i tried to wrap it  
into both single and double quotes but with no result:

location /oldfolder {  
rewrite ‘^/oldfolder/(\d{2})(\d{2})(\d{2})\_(\d).jpg$’  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)?  
permanent;  
}

location /oldfolder {  
rewrite “^/oldfolder/(\d{2})(\d{2})(\d{2})\_(\d).jpg$”  
[http://images.site2.com/newfolder/$1/$2/new\_images/$1$2$3/$1$2$3\_image\_$4\_large.jpg](http://images.site2.com/newfolder/$1/$2/new_images/$1$2$3/$1$2$3_image_$4_large.jpg)?  
permanent;  
}

Nginx doesn’t redirect my uri requests, so the problem still remains…

> — appa
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

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