Nginx从网址中删除PHP扩展名和索引 [英] Nginx remove php extension and index from url
问题描述
希望有人可以帮助我. 意识到;对不起,英语不好;) 我正试图花费数小时来设置我的配置,以便无需php扩展名即可访问我的网站,但是它将重定向任何带有扩展名的请求,并从URL中删除/index.
Hope someone can help me. be aware; sorry for possible bad english ;) I'm trying for hours to setup my configs so my site is accessible without php extension, but will redirect any request with extension and in addition remove the /index from URL.
我已经到了每个扩展都将被删除并且仍然被php5-fpm解析的地步.
i have come to the point where every extension will removed and still got parsed by php5-fpm.
这是我当前的代码:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.php;
<<unrelated Modulecode>>
rewrite ^(.*).php(.*)$ $1$2 permanent; # << forces index to be appended, so not exactly wanted
#rewrite ^/index$ / permanent; >> results in infinity loop for indexpage
# location ~ ^(.*)(?<!(index)).php(.*)$ { # << even don't know how to at least except index...gwarz regex x|
# rewrite ^(.*).php(.*)$ $1$2 permanent;
# }
location / {
try_files $uri $uri/ @phprule;
}
location ~ \.php$ {
try_files $uri $uri/ 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location @phprule {
rewrite ^(.*)$ $1.php last;
}
}
未注释的部分是一些绝望的尝试,以某种方式使其能够工作x<
the uncommented parts are some desperate trys to get it somehow to work x<
所以我想完成什么:
example.com/index.php => example.com/
example.com/index.php => example.com/
example.com/foo.php => example.com/foo
example.com/foo.php => example.com/foo
example.com/some/path/foo.php => example.com/some/path/foo
example.com/some/path/foo.php => example.com/some/path/foo
希望你会明白的.
推荐答案
可能找到了问题或继续进行下去,但是为了那些后来的人,这些小的变化对我有用:
Probably found the issue or moved on, but for the benefit of those who come along after, these little changes did the trick for me:
location @phprule {
rewrite ^/(.*)$ "/$1.php" last;
}
在正则表达式中添加了
/
,在重写字符串中添加了引号和正斜杠.
/
added to the regex, quotes and a forward slash added to the rewrite string.
这篇关于Nginx从网址中删除PHP扩展名和索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!