尝试使用htaccess删除.php扩展名

时间:2019-05-16 07:27:58

标签: php regex apache .htaccess

大家好,我正在练习如何在我的php页面中更改.php扩展名,还没有运气。我在64位上使用WAMP 3.1.0,此处是WAMP中的软件版本

  • Apache版本:2.4.27
  • PHP版本:5.6.31服务器软件:Apache / 2.4.27(Win64)
  • PHP / 5.6.31-为Apache定义的端口:80

我已打开rewrite_module 在httpd.conf文件中,我将此字符串“ LoadModule rewrite_module modules / mod_rewrite.so”注释掉,并将AllowOverride None更改为AllowOverride ALL。

下面是我的文件夹结构的屏幕截图

enter image description here

这是我的index.php文件的屏幕截图

enter image description here

这是我的htaccss屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以尝试这样做,以从文件中完全删除.php扩展名并避免无限循环:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

此代码可在Root / .htaccess中使用,如果要将其放置到子目录中的htaccess文件中,请确保更改RewriteBase。