Regular saying to match something and not match something

I want a nginx hosting directive that matches all urls with "xyz" and doesn't match ".php"

I tried this

location ~* /(.*)xyz/(.*)(?!\.php)(.*)$ {}

but, for example, it always seems to match both / xyz / 1 and / xyz / 1.php, but should only match / xyz / 1

+3
source share
5 answers

Thanks to everyone, we finally came up with this

location ~* ^/xyz([^.]*)/([^.]*)$ {
            rewrite  ^/xyz([^.]*)/([^.]*)$ /xyz$1/index.php/$2 last;
}

and it works.

0
source

You should be more specific about the beginning and end of the location. If .phpit should not appear at the end of the location, put $in the search confirmation:

location ~* /(.*)xyz/(.*)(?!.*\.php$)(.*)$ {}

xyz ( /xyz/), :

location ~* ^/xyz/(?!.*\.php$)(.*)$ {}
+2

, , , ".php" . , , .*, , , .

+1

, . *? ( "?" ) , .

... ( ):

[^.][^p][^h][^p]

, ... .

+1

It is simple, but it can be difficult to see. The second (. *) In the regular expression is "greedy", therefore, it will capture everything, including ".php", therefore for the "eyes" of the parsing is "no" .php after it (it has already passed) and the result is match .: (

Just do the second one. * lazy changing it to. *? and it will solve your problem:

location ~* /(.*)xyz/(.*?)(?!\.php)(.*)$ {}

I suggest you read this article , this explains it much better than me: D

+1
source

Source: https://habr.com/ru/post/1771083/


All Articles