# A regex for rewrite

**URL:** <https://www.ruby-forum.com/t/a-regex-for-rewrite/236461>\
**Category:** NGINX\
**Created:** [September 5, 2013, 12:54am UTC](https://www.ruby-forum.com/t/a-regex-for-rewrite/236461 "2013-09-05T00:54:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 5, 2013, 12:54am UTC](https://www.ruby-forum.com/t/a-regex-for-rewrite/236461/1 "2013-09-05T00:54:22Z")

</div>

I have a set of rewrites as

rewrite ^/(._)/(._)/(._).(._) /script.php?a=$1&b=$2&c=$3&ext=$4 last;  
rewrite ^/(._)/(._).(._) /script.php?a=$1&b=$2&ext=$3 last;  
rewrite ^/(._).(._) /script.php?a=$1&ext=$2 last;  
rewrite ^/(._)/(._)/(._)/(._) /script.php?a=$1&b=$2&c=$3&d=$4 last;  
rewrite ^/(._)/(._)/(._) /script.php?a=$1&b=$2&c=$3 last;  
rewrite ^/(._)/(._) /script.php?a=$1&b=$2 last;

How can I use one single rewrite rule to match all possible choices?

Posted at Nginx Forum:

> **[a regex for rewrite](https://forum.nginx.org/read.php?2,242547,242547#msg-242547)**
>
> I have a set of rewrites as rewrite ^/(.\*)/(.\*)/(.\*)\\.(.\*) /script.php?a=$1&b=$2&c=$3&ext=$4 last; rewrite ^/(.\*)/(.\*)\\.(.\*) /script.php?a=$1&b=$2&ext=$3 last; rewrite ^/(.\*)\\.(.\*) /script.php?a=$1&ext=$2 last; rewrite...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [September 5, 2013, 2:06am UTC](https://www.ruby-forum.com/t/a-regex-for-rewrite/236461/2 "2013-09-05T02:06:13Z")

</div>

On Wed, Sep 04, 2013 at 06:54:06PM -0400, etrader wrote:

Hi there,

> I have a set of rewrites as
> 
> rewrite ^/(._)/(._)/(._).(._) /script.php?a=$1&b=$2&c=$3&ext=$4 last;  
> rewrite ^/(._)/(._).(._) /script.php?a=$1&b=$2&ext=$3 last;  
> rewrite ^/(._).(._) /script.php?a=$1&ext=$2 last;  
> rewrite ^/(._)/(._)/(._)/(._) /script.php?a=$1&b=$2&c=$3&d=$4 last;  
> rewrite ^/(._)/(._)/(._) /script.php?a=$1&b=$2&c=$3 last;  
> rewrite ^/(._)/(._) /script.php?a=$1&b=$2 last;
> 
> How can I use one single rewrite rule to match all possible choices?

If you really want to do that, I’d suggest using a programming language  
to do complicated programming, and using nginx.conf for simpler things.

location ~ ^/.\*[/.] {  
rewrite ^ /script.php?E=READ\_THE\_REQUEST last;  
}

and then change script.php to process $\_SERVER[REQUEST\_URI] when it gets  
the special value E=READ\_THE\_REQUEST, so that it populates a, b, c, d,  
and ext as is appropriate.

In fact, I suspect that I’d not use rewrite at all: either fastcgi\_pass  
directly in this location; or proxy\_pass sending the original $uri as  
an argument.

(There are cases where this “location” is not a drop-in replacement for  
the initial "rewrite"s. If your system is one of those, you’ll need a  
different plan.)

But if your rewrite system works and is clear enough for you, you  
probably  
don’t need to change it.

Good luck with it,

## f

Francis D. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [September 5, 2013, 2:11am UTC](https://www.ruby-forum.com/t/a-regex-for-rewrite/236461/3 "2013-09-05T02:11:29Z")

</div>

On Thu, Sep 05, 2013 at 01:05:17AM +0100, Francis D. wrote:

Hmm…

> location ~ ^/.\*[/.] {

That’s probably more briefly written as

```
location ~ .[/.] {

```

## f

Francis D. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [September 5, 2013, 5:20pm UTC](https://www.ruby-forum.com/t/a-regex-for-rewrite/236461/4 "2013-09-05T17:20:59Z")

</div>

Thanks for very subtle suggestion. I do agree with you and follow this  
strategy (to use programming language for processing the url arguments).

Posted at Nginx Forum:

> **[Re: a regex for rewrite](https://forum.nginx.org/read.php?2,242547,242565#msg-242565)**
>
> I have a set of rewrites as rewrite ^/(.\*)/(.\*)/(.\*)\\.(.\*) /script.php?a=$1&b=$2&c=$3&ext=$4 last; rewrite ^/(.\*)/(.\*)\\.(.\*) /script.php?a=$1&b=$2&ext=$3 last; rewrite ^/(.\*)\\.(.\*) /script.php?a=$1&ext=$2 last; rewrite...
