我想用nginx重写网址。
样品:
/something.php (not regular file) -> /index.php?site=something
/somthingelse.php (regular file) -> /somethingelse.php
我目前的规则不起作用:
location / {
try_files $uri $uri/ @rules;
}
location @rules {
rewrite ^/([a-z]*)\.php$ /index.php?s=$1;
}
答案 0 :(得分:0)
您的/something.php
网址被location ~ \.php$
抓取,因此您需要在那里重写。
location ~ \.php$ {
try_files $uri @rules;
# usual php stuff
...;
}
location @rules {
rewrite ^/([a-z]*)\.php$ /index.php?s=$1 last;
return 404;
}
对于现有文件/somefile.php
,它将像往常一样由PHP处理。
对于不存在的/other.php
,它将在内部重定向到@rules
,在那里它将被重写为/index.php?s=other
并再次进入location ~ \.php
并最终由PHP处理。< / p>
对于不存在的/w31rd.php
(其中w31rd
与[a-z]*
正则表达式不匹配),您将收到404 Not Found
错误页面。