I have a prestashop instance that generates a random URL for admin access. The only rule is that the path starts with "admin".
The following rule works just fine, but its hardcoded by hand:
location /admin6908ewwh6/ {
if (!-e $request_filename) {
rewrite ^/.*$ /admin6908ewwh6/index.php last;
}
}
I tried to rewrite it into this:
location ^(/admin.*?)(\w+)/ {
if (!-e $request_filename) {
rewrite ^/.*$ $1/index.php last;
}
}
But this is not working, and I don't know why since according to this regex matcher ( https://www.regextester.com/102896 ) when I put a ^(/admin.*?)(\w+)
regex against a test string /admin6908ewwh6/index.php/sell/catalog/products/new?_token=_JC1fQPwgvwnhZTWyeGVTy4nET350GC4Aro888TuzDA&
it just grabs what I need to take.
Can somebody explain me why these two location blocks are not equivalent?
The problem is $1
. Numeric captures are assigned by the last regular expression to be evaluated, which in this case is the rewrite
statement (despite there being no parentheses in the regular expression).
One solution is to make the capture in the rewrite
statement, for example:
location /admin {
if (!-e $request_filename) {
rewrite ^(/admin[^/]+)/ $1/index.php last;
}
}
Or without the if
block:
location /admin {
try_files $uri $uri/ @admin;
}
location @admin {
rewrite ^(/admin[^/]+)/ $1/index.php last;
}
Or without the rewrite
statement:
location ~ ^(/admin[^/]+)/ {
try_files $uri $uri/ $1/index.php$is_args$args;
}
Ensure that this last location block is below the block that processes .php
URIs.