【问题标题】:URL 404s looking for index.php in a subdirectory (.htaccess)在子目录 (.htaccess) 中查找 index.php 的 URL 404
【发布时间】:2017-04-06 19:19:44
【问题描述】:

嘿,我正在尝试链接到子目录并在该目录中加载 index.php,但它返回 404 not found 错误。文件结构是这样的:

| index.php
| .htaccess
| subfolder
     | index.php
     | about.php

当我转到mysite.com/subfolder 时,我希望那里加载 index.php。我知道问题出在我的 .htaccess 文件上,因为当我删除它时,URL 有效。这是.htaccess中的代码:

Options +MultiViews

# Remove file extensions from page names

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

# Disable Index Listing
Options -Indexes

我的猜测是,因为我正在删除文件扩展名,服务器正在寻找 mysite/subfolder.php 并且找不到它,所以它是 404s。但是,我是 .htaccess 文件的新手,它们对我来说仍然很神秘。在线搜索有很多代码,但没有解释任何东西是如何工作的。任何关于如何解决此问题以及为什么该修复有效的想法都会有所帮助!

【问题讨论】:

    标签: php html apache .htaccess


    【解决方案1】:

    如果您启用MultiViews 选项,那么您不需要重写规则来处理扩展。

    只需将这些行保留在您的 .htaccess 中即可:

    Options +MultiViews
    DirectorySlash On
    
    DirectoryIndex index.php
    

    并删除其他所有内容。然后用mysite.com/subfolder/ URL 测试

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 1970-01-01
      • 2017-02-21
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2016-02-04
      • 1970-01-01
      相关资源
      最近更新 更多