我希望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,但返回404,例如2,其中包含更复杂的URL。
您可以使用带有location
指令的命名try_files
来实现一个或多个rewrite
语句。有关详细信息,请参阅this document。
例如:
location / {
try_files $uri $uri/ $uri.html @php;
}
location @php {
rewrite ^(/[^/]+)$ $1.php last;
rewrite ^(/[^/]+)/(.*)$ $1.php?q=$2 last;
}
location ~ \.php$ {
try_files $uri =404;
...
}
rewrite
语句按顺序进行评估。第二个try_files
语句确保PHP文件实际存在并且避免使用passing uncontrolled requests to PHP。