【问题标题】:Remove .php extension with htaccess, but retain /index.php functionality使用 htaccess 删除 .php 扩展名,但保留 /index.php 功能
【发布时间】:2018-08-27 13:02:48
【问题描述】:

我正在尝试使用以下内容删除所有 .php 扩展名...

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

但是我有一个目录 - mysite.com/example/,它将不再显示 index.php 文件,相反,我必须访问 mysite.com/example/index。我确定必须有解决方法吗?谢谢

编辑:目的是能够访问 mysite.com/example/ 并正常加载索引页面。但是对于诸如 mysite.com/example/test.php 之类的文件,.php 扩展名将被删除,并将从链接 mysite.com/example/test 加载

【问题讨论】:

标签: php .htaccess


【解决方案1】:

请将此代码添加到 .htaccess 文件中

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

Options -Indexes
DirectoryIndex index.php index.html

注意:以上选项和目录索引行允许加载特定文件,以防万一没有设置默认索引文件

【讨论】:

  • 我认为 DirectoryIndex 也应该包含索引,所以它变成:DirectoryIndex index.php index.html index
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 2018-12-04
  • 2011-03-02
  • 2011-04-30
相关资源
最近更新 更多