# Having trouble with the PCRE syntax

**URL:** <https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421>\
**Category:** NGINX\
**Created:** [March 11, 2011, 4:02pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421 "2011-03-11T16:02:17Z")\
**Posts on this page:** 8\
**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:** [March 11, 2011, 4:02pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421/1 "2011-03-11T16:02:17Z")

</div>

Hi,

would anybody please tell me what I’m doing wrong here?

location ~\* ^/(forum|forums|board)/? {  
rewrite ^/([^/]_)/_(.\*) http://forum.my\_domain.com/$2  
permanent;  
}

I’d like to make it redirect visitors from  
http://www.my\_domain.com/forum (note: there’s no trailing slash),  
http://www.my\_domain.com/forum/, http://www.my\_domain.com/forums (again,  
there’s no trailing slash here), http://www.my\_domain.com/forums/,  
http://www.my\_domain.com/board (…yep) and  
http://www.my\_domain.com/board/ to http://forum.my\_domain.com/. Neither  
the path (except “forum”/“forums”/“board”) nor the query (if there’s  
one) should get cut off. It’s working so far, but it also redirects  
visitors from ex. http://www.my\_domain.com/forumfoobar/, which isn’t  
what I want. ☹

Thanks in advance!

Posted at Nginx Forum:

> **[Having trouble with the PCRE syntax](https://forum.nginx.org/read.php?2,182278,182278#msg-182278)**
>
> Hi, would anybody please tell me what I'm doing wrong here? location ~\* ^/(forum|forums|board)/? {      rewrite ^/(\[^/\]\*)/\*(.\*) http://forum.my\_domain.com/$2 permanent; } I'd like to make it redirect visitors from http://www.my\_domain.c

---

<div class="post-metadata">

**Author:** ![strike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@strike](https://www.ruby-forum.com/u/strike)\
**Post date:** [March 11, 2011, 4:11pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421/2 "2011-03-11T16:11:42Z")

</div>

Hi,

i’d write the end more like (/?|$), this would match if there  
is a /, or if there is the end - i think.

Regards,  
Sven

Am 11.03.2011 16:01, schrieb strike:

---

<div class="post-metadata">

**Author:** ![strike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@strike](https://www.ruby-forum.com/u/strike)\
**Post date:** [March 11, 2011, 4:27pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421/3 "2011-03-11T16:27:24Z")

</div>

Thanks for your reply! But http://www.my\_domain.com/forumfoobar/ still  
redirects to http://forum.my\_domain.com/. I don’t want that. ☹

## Sven ‘Darkman’ Michels Wrote:

> > Hi,  
> > I’d like to make it redirect visitors from  
> > the query (if there’s  
> > g-182278  
> > [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,182278,182284#msg-182284](http://forum.nginx.org/read.php?2,182278,182284#msg-182284)

---

<div class="post-metadata">

**Author:** ![strike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@strike](https://www.ruby-forum.com/u/strike)\
**Post date:** [March 11, 2011, 4:41pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421/4 "2011-03-11T16:41:11Z")

</div>

On Fri, Mar 11, 2011 at 10:01:03AM -0500, strike wrote:

Hi there,

> would anybody please tell me what I’m doing wrong here?
> 
> location ~\* ^/(forum|forums|board)/? {

You ask for /, forum, then one-or-no slash.

> It’s working so far, but it also redirects  
> visitors from ex. http://www.my\_domain.com/forumfoobar/, which isn’t  
> what I want. ☹

The second “f” in “forumfoobar” is one-or-no slash. (Specifically,  
it is no slash). So it matches.

You want to ask for /, forum, then either a slash or end-of-string.

location ~\* ^/(forum|forums|board)(/|$) {

(You could also abbreviate forum|forums to forums?, should you so  
choose.)

Good luck,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![strike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@strike](https://www.ruby-forum.com/u/strike)\
**Post date:** [March 11, 2011, 4:49pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421/5 "2011-03-11T16:49:22Z")

</div>

On Fri, Mar 11, 2011 at 03:40:08PM +0000, Francis D. wrote:

> On Fri, Mar 11, 2011 at 10:01:03AM -0500, strike wrote:

Hi there,

A minor correction in the explanation:

> > location ~\* ^/(forum|forums|board)/? {
> 
> You ask for /, forum, then one-or-no slash.
> 
> > It’s working so far, but it also redirects  
> > visitors from ex. http://www.my\_domain.com/forumfoobar/, which isn’t  
> > what I want. ☹
> 
> The second “f” in “forumfoobar” is one-or-no slash. (Specifically,  
> it is no slash). So it matches.

Strictly, it’s more like the region between the m and the f that is the  
“no slash”, rather than the “f” itself.

But the pattern still matches 🙂

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![strike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@strike](https://www.ruby-forum.com/u/strike)\
**Post date:** [March 11, 2011, 6:34pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421/6 "2011-03-11T18:34:39Z")

</div>

Hello!

On Fri, Mar 11, 2011 at 10:01:03AM -0500, strike wrote:

> http://www.my\_domain.com/forum (note: there’s no trailing slash),  
> http://www.my\_domain.com/forum/, http://www.my\_domain.com/forums (again,  
> there’s no trailing slash here), http://www.my\_domain.com/forums/,  
> http://www.my\_domain.com/board (…yep) and  
> http://www.my\_domain.com/board/ to http://forum.my\_domain.com/. Neither  
> the path (except “forum”/“forums”/“board”) nor the query (if there’s  
> one) should get cut off. It’s working so far, but it also redirects  
> visitors from ex. http://www.my\_domain.com/forumfoobar/, which isn’t  
> what I want. ☹

Correct regexp-based solution would be

```
location ~* ^/(?:forum|forums|board)(?:/|$) {
    rewrite ^/[^/]*/*(.*) http://forum.my_domain.com/$2 permanent;
}

```

But recommended way is to avoid regexp-based solutions without  
real need. It’s much simplier and much more scalable to write  
configuration with normal static locations, i.e.

```
location = /forum {
    rewrite ^ http://forum.my_domain.com/ permanent;
}
location /forum/ {
    rewrite ^/forum/(.*) http://forum.my_domain.com/$1 permanent;
}
location = /board {
    rewrite ^ http://forum.my_domain.com/ permanent;
}
location /board/ {
    rewrite ^/board/(.*) http://forum.my_domain.com/$1 permanent;
}

```

This way you’ll save yourself a headache. As a side effect - it’s  
faster.

Maxim D.

---

<div class="post-metadata">

**Author:** ![strike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@strike](https://www.ruby-forum.com/u/strike)\
**Post date:** [March 11, 2011, 6:48pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421/7 "2011-03-11T18:48:26Z")

</div>

On Mar 11, 2011, at 20:33 , Maxim D. wrote:

> location = /forum {  
> }
> 
> This way you’ll save yourself a headache.

[ when configuration will eventually grow.  
And it will certainly grow eventually. ]

> As a side effect - it’s  
> faster.
> 
> Maxim D.

–  
Igor S.

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

---

<div class="post-metadata">

**Author:** ![strike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@strike](https://www.ruby-forum.com/u/strike)\
**Post date:** [March 11, 2011, 9:40pm UTC](https://www.ruby-forum.com/t/having-trouble-with-the-pcre-syntax/204421/8 "2011-03-11T21:40:48Z")

</div>

Hello,

thank you guys and thanks a bunch for the explanation!

I’ll stick to what Maxim suggested, because it will definitively save me  
from headaches. But it’s good to have the explanation and so to  
understand what the problem was as the syntax looks like fundamental  
knowledge to me.

Wow, even the creator himself spent some of his very spare time in  
writing a reply. 😃

Thanks again!

Posted at Nginx Forum:

> **[Re: Having trouble with the PCRE syntax](https://forum.nginx.org/read.php?2,182278,182349#msg-182349)**
>
> Hi, would anybody please tell me what I'm doing wrong here? location ~\* ^/(forum|forums|board)/? {      rewrite ^/(\[^/\]\*)/\*(.\*) http://forum.my\_domain.com/$2 permanent; } I'd like to make it redirect visitors from http://www.my\_domain.c
