Remove .php extensions with .htaccess without breaking DirectoryIndex

亡梦爱人 提交于 2019-12-17 03:18:07

问题


I have the following rewrite in my .htaccess file which removes the .php extension from files, converting for example so.com/question.php to so.com/question.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

However this also breaks the default DirectoryIndex behaviour, in which just typing the directory will redirect to the index file in the folder, e.g. so.com/answer displays so.com/answer/index.php

Simply combining the above code with DirectoryIndex index.php does not achieve both results.

Can someone help me combine these two functions, or rewrite the code to exclude index.php files, which would achieve the same result?


回答1:


I'm thinking you just need to verify that the file exists prior to doing the rewrite, that way you'll leave 404 and directoryindex behaviours intact:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

(not tested)




回答2:


RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]

verify files and folder and also, add RewriteBase /




回答3:


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

I tested and it is working fine.



来源:https://stackoverflow.com/questions/9635524/remove-php-extensions-with-htaccess-without-breaking-directoryindex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!