Nginx rewrite if specific name is included
I would like to rewrite all requests containing "xhr.php" in /xhr.php instead of the original path.
Request:
type: "POST",
url: "/system/classes/templates/address/xhr.getAddress.php",
data: vars,
dataType: "html"
Should go to /xhr.php with path as argument. I've tried this:
rewrite ^/(.*)/xhr.(.*)\.php$ /xhr.php?parameter=$1¶meter2=$2 last;
However, this will not work. Any ideas? My configuration looks like this:
location / {
root /var/www/http;
try_files $uri $uri/ index.php /index.php$is_args$args @rewrite;
}
location @rewrite {
rewrite ^/(.*)/xhr.(.*)\.php$ /xhr.php?parameter=$1¶meter2=$2 last;
}
Is this possible with nginx? What's wrong here? Thank:)
// EDIT: I solved it, however it doesn't look very efficient ...
location / {
root /var/www/http;
try_files $uri $uri/ index.php /index.php$is_args$args;
}
if ($request_uri ~ .*/xhr.*) {
rewrite ^/(.*)/xhr.(.*)\.php$ /xhr.php?parameter=$1¶meter2=$2 break;
}
+3
source to share
1 answer
You can remove the if and allow rewriting at the same location level. This way you store one regex for each request (one inside the if).
In fact, I think you can also remove location /
:
root /var/www/http;
rewrite ^/(.*)/xhr.(.*)\.php$ /xhr.php?parameter=$1¶meter2=$2 break;
try_files $uri $uri/ index.php /index.php$is_args$args;
+2
source to share