# 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:

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 ~ ^/.*[/.] {
}

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:
{"ft_lang_label":"__label__en","ft_lang_prob":0.7462624,"math_prob":0.7256188,"size":2311,"snap":"2023-14-2023-23","text_gpt3_token_len":747,"char_repetition_ratio":0.21369745,"word_repetition_ratio":0.27272728,"special_character_ratio":0.37256598,"punctuation_ratio":0.25945947,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99001145,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-26T06:24:49Z\",\"WARC-Record-ID\":\"<urn:uuid:92551773-bb59-425c-8111-b3313dc84728>\",\"Content-Length\":\"22370\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6770b921-ec28-43cd-a173-f6995c25c5ed>\",\"WARC-Concurrent-To\":\"<urn:uuid:1dad39fd-6121-4324-ba32-07af58242179>\",\"WARC-IP-Address\":\"172.66.43.102\",\"WARC-Target-URI\":\"https://www.ruby-forum.com/t/a-regex-for-rewrite/236461\",\"WARC-Payload-Digest\":\"sha1:MTEQQA4EO6UCARWPQWKSZM6TIJEI2EI5\",\"WARC-Block-Digest\":\"sha1:AYUTRGCKA3IREB5Q4QGD65JBVSLVW6LG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945433.92_warc_CC-MAIN-20230326044821-20230326074821-00021.warc.gz\"}"}