# String replace on rewrite module

**URL:** <https://www.ruby-forum.com/t/string-replace-on-rewrite-module/191905>\
**Category:** NGINX\
**Created:** [July 20, 2010, 3:23pm UTC](https://www.ruby-forum.com/t/string-replace-on-rewrite-module/191905 "2010-07-20T15:23:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbruni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jbruni](https://www.ruby-forum.com/u/jbruni)\
**Post date:** [July 20, 2010, 3:23pm UTC](https://www.ruby-forum.com/t/string-replace-on-rewrite-module/191905/1 "2010-07-20T15:23:55Z")

</div>

My goal is simple. Rewrite the following URL:

[b][http://example.com/path/to/folder/image/picture1.jpg[/b]](http://example.com/path/to/folder/image/picture1.jpg%5B/b%5D)

to

[b][http://example.com/image/path\_to\_folder/picture1.jpg[/b]](http://example.com/image/path_to_folder/picture1.jpg%5B/b%5D)

My doubt is: how can I transform path/to/folder into  
path\_to\_folder?

In fact, all I want to know is how to substitute the forward slashes  
“/” by underscores “\_”

The path/to/folder can be nested in unknown levels (as in  
path/to/a/very/deep/level/folder )

Is there a “string replace” function in rewrite module to do this? Or  
something else?

Thanks!

Posted at Nginx Forum:

> **[String replace on rewrite module](https://forum.nginx.org/read.php?2,110833,110833#msg-110833)**
>
> My goal is simple. Rewrite the following URL: \[b\]http://example.com/path/to/folder/image/picture1.jpg\[/b\] to \[b\]http://example.com/image/path\_to\_folder/picture1.jpg\[/b\] My doubt is: how can I transform \[b\]path/to/folder\[/b\] into \[b\]path\_to\_

---

<div class="post-metadata">

**Author:** ![jbruni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jbruni](https://www.ruby-forum.com/u/jbruni)\
**Post date:** [July 20, 2010, 3:36pm UTC](https://www.ruby-forum.com/t/string-replace-on-rewrite-module/191905/2 "2010-07-20T15:36:13Z")

</div>

JBruni,

Eventually you’ll need something like:

rewrite ^(/._)/(._)/(._)/image/(._)$ $1\_$2\_$3/$4 break;

More info available at: [Module ngx\_http\_rewrite\_module](http://wiki.nginx.org/NginxHttpRewriteModule)

Alex.

---

<div class="post-metadata">

**Author:** ![jbruni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jbruni](https://www.ruby-forum.com/u/jbruni)\
**Post date:** [July 20, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/string-replace-on-rewrite-module/191905/3 "2010-07-20T15:40:19Z")

</div>

Oh, missed part about nestedness to more levels…

You just can write multiple rewrites for 1 level, 2 levels and so on for  
up to 9 levels deep. Would that be enough?

You can also build map of all possible redirects from all "virtual  
folder names to “underscored” and use it faster than rewrite everything  
at once.

You can also build recursive redirects like in my example, without break  
to make nginx repeat cycle of rewrites again, up to 10 times.

Alex

---

<div class="post-metadata">

**Author:** ![jbruni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jbruni](https://www.ruby-forum.com/u/jbruni)\
**Post date:** [July 21, 2010, 6:33am UTC](https://www.ruby-forum.com/t/string-replace-on-rewrite-module/191905/4 "2010-07-21T06:33:45Z")

</div>

2010/7/20 jbruni [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Anyway, a “string replace” function for the rewrite module remains a  
> wish as a feature request…

I think there’s embedded perl for that kind of stuff, but i can’t help  
because i don’t know perl 🙂

---

<div class="post-metadata">

**Author:** ![jbruni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jbruni](https://www.ruby-forum.com/u/jbruni)\
**Post date:** [July 20, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/string-replace-on-rewrite-module/191905/5 "2010-07-20T16:58:48Z")

</div>

## Alex S. Wrote:

> You can also build recursive redirects like in my  
> example, without break  
> to make nginx repeat cycle of rewrites again, up  
> to 10 times.

Thanks a lot, Alex,

Your comments enlightened my path!

Here is the working configuration:

```auto
  location ~ ^/(.*)/([^/]+)/image/([^/]*)$ {
    set $part1 $1;
    set $part2 $2;
    set $filename $3;
    rewrite ^.*$ /${part1}_$part2/image/$filename last;
  }

  location ~ ^/([^/]+)/image/([^/]*)$ {
    set $folder $1;
    set $filename $2;
    rewrite ^.*$ /image/$folder/$filename break;
  }
```

The first location directive is recursive, and keeps joining the paths,  
replacing “/” by “\_” on each iteration.

The second location directive is the final one. When all path pieces are  
already joined, it does the final rewrite.

Cool!

Anyway, a “string replace” function for the rewrite module remains a  
wish as a feature request…

Thanks!

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

---

<div class="post-metadata">

**Author:** ![jbruni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jbruni](https://www.ruby-forum.com/u/jbruni)\
**Post date:** [July 21, 2010, 3:35pm UTC](https://www.ruby-forum.com/t/string-replace-on-rewrite-module/191905/6 "2010-07-21T15:35:53Z")

</div>

On Wed, 2010-07-21 at 10:32 +0600, Mikhail Mazursky wrote:

> I think there’s embedded perl for that kind of stuff, but i can’t help  
> because i don’t know perl 🙂

Yeah… something like this would also work

perl\_set $underscored\_uri ‘sub { ($\_ = $_[0]-\>uri) =~  
s/(?\<=.)/(?=.\*/)/_/g; $\_ }’

location /something {  
rewrite . $underscored\_uri last;  
}

1. you’re ending up with perl module which is big
2. could be trickier if you have querystring to process
3. likely it’ll be slower than recursive rewrites.

Alex.

p.s.  
if you want to debug this in command line:

perl -Mstrict -we ‘sub uri{$ARGV[0]} sub test{($_=uri()) =~  
s/(?\<=.)/(?=.\*/)/_/g;$\_} print  
test().“\n”’ /lkasld/asda/sda/sd/image.gif

will print you  
/lkasld\_asda\_sda\_sd/image.gif
