【问题标题】:.htaccess - DirectoryIndex appears to have no effect.htaccess - DirectoryIndex 似乎没有效果
【发布时间】:2018-05-18 11:20:02
【问题描述】:

我正在尝试在我的 CodeIgniter 安装的根目录中调整 .htaccess 文件,因此我不必在通向我的控制器的所有 URL 中包含 index.php。

这是我的 .htaccess 文件的内容:

<IfModule authz_core_module>
    Require all granted
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

DirectoryIndex login.php

RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteCond $(REQUEST_FILENAME) !-f
RewriteCond $(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1

如您所见,它是默认的 .htaccess 文件,其中包含一些用于 mod_rewrite 的重写规则和附加的 DirectoryIndex 指令。 Mod_rewrite 已启用(我已使用 apachectl -M 进行了检查)。

现在,我在 Ubuntu 16.04 LTS 上运行 Apache 2.4.18,我正在使用 CodeIgniter 3.1.6。问题是当我没有输入任何 URL 时,我会被重定向到 CodeIgniters index.php 而不是我想要的 login.php。出于显而易见的原因,我宁愿让 DirectoryIndex 指令正常工作,而不是将 login.php 的内容传输到 index.php 中。我的 mod_rewrite 规则旨在“通过”index.php 将 URL 路由到现有文件,而不必在其 URL 中包含 index.php。这样,我的 URL 更清晰,但我仍然可以使用 CodeIgniter 的工具。

我的主 Apache 配置显然允许我使用 .htaccess 文件(因为 CodeIgniter 也需要这个)并且 URL 重写指令工作得很好。为什么无论我将 DirectoryIndex 放在文件中的哪个位置,它似乎都没有做任何事情?

【问题讨论】:

标签: php apache .htaccess codeigniter


【解决方案1】:

检查您是否将 AllowOverride 指令设置为 All -- 它控制 .htaccess 文件是否有任何权力。

如何设置有答案:How to Set AllowOverride all

【讨论】:

    【解决方案2】:

    我解决了这个问题,但不是以一种优雅的方式。

    在我看来,当您在 DocumentRoot 的子目录的 .htaccess 文件中使用 mod_rewrite 时(正确)它不起作用。

    我的 CodeIgniter 安装在 DocumentRoot 内一个名为“CodeIgniter”的子目录中。我制作了两个 .htaccess 文件:

    DocumentRoot/.htaccess:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %1 !^index.php
    RewriteRule ^(.*)$ /CodeIgniter/index.php [L]
    

    DocumentRoot/CodeIgniter/.htaccess:

    <IfModule authz_core_module>
        Require all granted
    </IfModule>
    <IfModule !authz_core_module>
        Deny from all
    </IfModule>
    Options FollowSymLinks
    DirectoryIndex login.php
    

    这似乎对我有用

    故事寓意:使用位于 DocumentRoot 中直接的 .htaccess 中的 mod_rewrite 进行所有 URL 重写

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 2010-12-24
      • 2020-07-04
      • 2014-11-06
      • 2020-04-11
      相关资源
      最近更新 更多