我一直在尝试在URL中添加'.php'文件扩展名。因此,如果我有一个URL,例如'www.example.com/page.php','www.example.com/page'也将有效。我设法做到了(本文和这个堆叠的问题是我尝试从哪里采取解决方案的地方(。
以下是我在文章页面上找到的解决方案:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
但是有一个问题,因为我的URL类似于' www.example.com/articles/index.php '。而' www.example.com/articles/index '工作,'要添加DirectoryIndex(带有' index.php '的值,甚至是' index '(。
是否有其他方法隐藏诸如'.php'之类的文件扩展名,同时仍使索引文件按预期工作?
您可以使用:
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]
谁测试PHP文件是否存在
您可以做到这一点:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [NE,L]
只有一个文件测试而不是三个文件,因此比另一个答案更有效。