# Regex rewrite problem

**URL:** <https://www.ruby-forum.com/t/regex-rewrite-problem/215895>\
**Category:** NGINX\
**Created:** [January 18, 2012, 11:02pm UTC](https://www.ruby-forum.com/t/regex-rewrite-problem/215895 "2012-01-18T23:02:46Z")\
**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 18, 2012, 11:02pm UTC](https://www.ruby-forum.com/t/regex-rewrite-problem/215895/1 "2012-01-18T23:02:46Z")

</div>

Hello,

I am new to this forum. Glad it exists. Thanks for that. I have a  
production website that uses nginx 0.8.53 reverse proxy in front of  
apache. I actually had little problem setting that up, but this simple  
regex is giving me a headache.

For some reason, I am unable to take a URI like  
[example.com/manufacturers/metlox/0?page=13](http://example.com/manufacturers/metlox/0?page=13) and change it to  
[example.com/manufacturers/m](http://example.com/manufacturers/m) It seems like the curly braces aren’t  
working.

I’ve tried the following without results:

location ~\* /manufacturers/ {  
rewrite "^/manufacturers/(.{1})/(._)$"  
/manufacturer/directory/$1 redirect;  
}  
location ~_ /manufacturers/ {  
rewrite “^/manufacturers/([a-z] {1})/(.\*)$”  
/manufacturer/directory/$1 redirect;  
}

location ~\* /manufacturers/ {  
rewrite “^/manufacturers/([a-z]?)/(.\*)$”  
/manufacturer/directory/$1 redirect;  
}

I am fairly new to regex, so maybe this is something simple. What I want  
to get is the first letter of first captured word. I am able to pull  
the entire word with (.\*) for $1, but not the first letter only.

Any help would be greatly appreciated!

Thanks in advance.

Posted at Nginx Forum:

> **[regex rewrite problem](https://forum.nginx.org/read.php?2,221410,221410#msg-221410)**
>
> Hello, I am new to this forum. Glad it exists. Thanks for that. I have a production website that uses nginx 0.8.53 reverse proxy in front of apache. I actually had little problem setting that up, but this simple regex is giving me a headache. For...

---

<div class="post-metadata">

**Author:** ![mpratt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mpratt](https://www.ruby-forum.com/u/mpratt)\
**Post date:** [January 18, 2012, 11:33pm UTC](https://www.ruby-forum.com/t/regex-rewrite-problem/215895/2 "2012-01-18T23:33:36Z")

</div>

> working.  
> }  
> Any help would be greatly appreciated!
> 
> Thanks in advance.

Hello.

Try the following:

location /manufacturers/ {  
location ~ ^/manufacturers/(.) {  
return 301 /manufacturers/$1;  
}  
}

---

<div class="post-metadata">

**Author:** ![mpratt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mpratt](https://www.ruby-forum.com/u/mpratt)\
**Post date:** [January 19, 2012, 1:32am UTC](https://www.ruby-forum.com/t/regex-rewrite-problem/215895/3 "2012-01-19T01:32:22Z")

</div>

Brilliant! It works perfectly. Do you have any idea why the curly braces  
didn’t work and also where I can learn when to use a location in a  
location? Thank you so much!

Posted at Nginx Forum:

> **[Re: regex rewrite problem](https://forum.nginx.org/read.php?2,221410,221414#msg-221414)**
>
> Hello, I am new to this forum. Glad it exists. Thanks for that. I have a production website that uses nginx 0.8.53 reverse proxy in front of apache. I actually had little problem setting that up, but this simple regex is giving me a headache. For...

---

<div class="post-metadata">

**Author:** ![mpratt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mpratt](https://www.ruby-forum.com/u/mpratt)\
**Post date:** [January 19, 2012, 2:48am UTC](https://www.ruby-forum.com/t/regex-rewrite-problem/215895/4 "2012-01-19T02:48:44Z")

</div>

On 19 Jan 2012 00h31 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Brilliant! It works perfectly. Do you have any idea why the curly  
> braces didn’t work and also where I can learn when to use a location  
> in a location? Thank you so much!

Regexes with curly braces have to be quoted.

From the 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)

```
 Note: for curly braces ('{' and '}'), as they are used both in
 regexes and for block control, to avoid conflicts, regexes with
 curly braces are to be enclosed with double quotes (or single
 quotes). For example, to rewrite URLs like

 rewrite "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" 

```

/path/to/photos/$1/$1$2/$1$2$3.png;

— appa

---

<div class="post-metadata">

**Author:** ![mpratt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mpratt](https://www.ruby-forum.com/u/mpratt)\
**Post date:** [January 19, 2012, 4:55am UTC](https://www.ruby-forum.com/t/regex-rewrite-problem/215895/5 "2012-01-19T04:55:37Z")

</div>

On Thu, Jan 19, 2012 at 10:46 AM, mpratt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> everything according to that section…

Of course it won’t. The regex above will only match url like this:

/manufacturers/a/something

and not:

/manufacturers/asdfgh/something

Because /(.{1})/ will only match (and capture) one character between  
two slashes (e.g. /u/, /b/) while /(.).\*/ will match (and capture)  
first character of word between two slashes (e.g. /unix/, /books/).

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

---

<div class="post-metadata">

**Author:** ![mpratt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mpratt](https://www.ruby-forum.com/u/mpratt)\
**Post date:** [January 19, 2012, 4:47am UTC](https://www.ruby-forum.com/t/regex-rewrite-problem/215895/6 "2012-01-19T04:47:01Z")

</div>

Thanks for your reply. I had previously read that section and enclosed  
them in quotes and it didn’t work:  
for example:

> location ~\* /manufacturers/ {  
> rewrite “^/manufacturers/(.{1})/(.\*)$” /manufacturer/directory/$1  
> redirect;  
> }

doesn’t work. That;'s why I was so stumped. Thought I had done  
everything according to that section…

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

---

<div class="post-metadata">

**Author:** ![mpratt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mpratt](https://www.ruby-forum.com/u/mpratt)\
**Post date:** [January 19, 2012, 2:30pm UTC](https://www.ruby-forum.com/t/regex-rewrite-problem/215895/7 "2012-01-19T14:30:43Z")

</div>

Thank you for this explanation!

Posted at Nginx Forum:

> **[Re: regex rewrite problem](https://forum.nginx.org/read.php?2,221410,221449#msg-221449)**
>
> Hello, I am new to this forum. Glad it exists. Thanks for that. I have a production website that uses nginx 0.8.53 reverse proxy in front of apache. I actually had little problem setting that up, but this simple regex is giving me a headache. For...
