NGINX 使用查询参数重写文件
NGINX rewrite to file with query parameters
我希望nginx将url重写为特定的php文件,可以通过第一个斜杠前的内容来确定
例如:
testing.com/test
或 test.com/test
将被重写为 test.php
testing.com/test2/variable/another-variable
将被重写为 /test2.php?q=variable/another-variable
然后我会使用 PHP 来分解 q GET
参数。
目前我尝试过的是:
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
这适用于我在上面显示的示例 1,但是 returns 示例 2 的 404 更复杂 URL。
您可以使用带有 try_files
指令的命名 location
来实现一个或多个 rewrite
语句。有关详细信息,请参阅 this document。
例如:
location / {
try_files $uri $uri/ $uri.html @php;
}
location @php {
rewrite ^(/[^/]+)$ .php last;
rewrite ^(/[^/]+)/(.*)$ .php?q= last;
}
location ~ \.php$ {
try_files $uri =404;
...
}
rewrite
语句按顺序计算。第二个 try_files
语句确保 PHP 文件实际存在并且避免 passing uncontrolled requests to PHP。
我希望nginx将url重写为特定的php文件,可以通过第一个斜杠前的内容来确定
例如:
testing.com/test
或test.com/test
将被重写为test.php
testing.com/test2/variable/another-variable
将被重写为/test2.php?q=variable/another-variable
然后我会使用 PHP 来分解 q GET
参数。
目前我尝试过的是:
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
这适用于我在上面显示的示例 1,但是 returns 示例 2 的 404 更复杂 URL。
您可以使用带有 try_files
指令的命名 location
来实现一个或多个 rewrite
语句。有关详细信息,请参阅 this document。
例如:
location / {
try_files $uri $uri/ $uri.html @php;
}
location @php {
rewrite ^(/[^/]+)$ .php last;
rewrite ^(/[^/]+)/(.*)$ .php?q= last;
}
location ~ \.php$ {
try_files $uri =404;
...
}
rewrite
语句按顺序计算。第二个 try_files
语句确保 PHP 文件实际存在并且避免 passing uncontrolled requests to PHP。