A regex for rewrite

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:
http://forum.nginx.org/read.php?2,242547,242547#msg-242547

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. [email protected]

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. [email protected]

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:
http://forum.nginx.org/read.php?2,242547,242565#msg-242565

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs