从htdocs中的文件中删除.php扩展名

时间:2016-11-08 23:57:24

标签: php apache .htaccess mod-rewrite

我的项目位于htdocs文件夹中,我的主文件为index.php。我已经尝试多次在浏览器中打开文件时删除.php扩展名,使用带有以下行的.htaccess文件,但没有运气:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]

延期仍然存在。是因为浏览器中的链接是

file:///C:/wamp64/bin/apache/apache2.4.23/htdocs/Cipcpr/index.php

而不是

localhost/... 

3 个答案:

答案 0 :(得分:3)

您可以使用:

Options -MultiViews
RewriteEngine on

# remove php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# rewrite with php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]

答案 1 :(得分:1)

如果您的项目不需要动态删除扩展程序,则可以为每个文件手动设置RewriteRules。这是index.php的代码

RewriteEngine On
RewriteRule ^index/?$ index.php [NC,L]

当你现在输入/ index时,它将引用/index.php文件。

要添加其他文件(让我们说login.php),您只需复制最后一个RewriteRule并重写索引即可。

RewriteRule ^login/?$ login.php [NC,L]

答案 2 :(得分:1)

只需将.htaccess文件添加到您网站的根文件夹(例如/home/domains/domain.com/htdocs/),其中包含以下内容:

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