a regex for rewrite

Francis Daly francis at daoine.org
Thu Sep 5 00:05:17 UTC 2013


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 Daly        francis at daoine.org



More information about the nginx mailing list