Nginx重写删除.php文件没有效果,只能redirect到主页

我试图重写从我的url中删除.php,但迄今没有运气。 这是我到目前为止,然后我的结果下面的这个configuration。

location ~ \.php$ { try_files $uri/ @extensionless-php; include fastcgi_params; fastcgi_pass hhvm; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache ASPS; fastcgi_cache_valid 200 60m; } location @extensionless-php { rewrite ^(.*)$ /$1.php last; } 

这导致:

example.com/page1.php转到example.com/page1.php并正确渲染example.com/page1渲染index.php example.com/page1/渲染index.php

有没有人遇到过这个? 这似乎是一个非常普遍的事情,但我有几个小时的麻烦,并无休止地在网上search。

谢谢你的帮助!

您的location ~ \.php$ block不会显示像example.com/page1这样的URI,因为它们与正则expression式不匹配。

你应该把你的try_files $uri/ @extensionless-php; 指令到不同的位置 – 通常是location /除了.php结尾的所有URI 之外 )。

尝试这样的事情:

 location / { try_files $uri $uri/ @extensionless-php; } location ~ \.php$ { if ($request_uri ~ (.*)\.php$) { return 301 $1; } try_files $uri =404; include fastcgi_params; fastcgi_pass hhvm; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache ASPS; fastcgi_cache_valid 200 60m; } location @extensionless-php { rewrite ^(.*)$ $1.php last; } 

编辑:为了删除以.php结尾的URL扩展名, location ~ \.php$块需要区分从外部提供的URL和内部重写。 实现这一点的一个方法是用if块testing$request_uri 。 请谨慎使用,因为外部重写.phpurl可能会破坏某些应用程序,特别是在发布表单时。