apache - .htaccess 未从 DocumentRoot 目录中正确读取

标签 apache .htaccess codeigniter mod-rewrite centos

我在/var/www/html 中安装了一个 codeigniter 项目。 URL 应如下所示:http://example.com/clients/abc 我正在运行 CentOS 和 Apache 2.2.15

为了让路由正常工作,我在 httpd.conf 中启用了 mod_rewrite(我可以看到它是通过 phpinfo.php 启用的)和 AllowOverride All。

我在 httpd.conf 中定义的 documentroot 是/var/www/html。

/var/www/html 中的 .htaccess 文件是:

<IfModule mod_rewrite.c>
    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>

就此本身,我在所有 Controller 上得到 404 个页面,除了索引之外,我的 Controller / View 都没有呈现。

但是。如果我在/var/www 中创建第二个 .htaccess 文件并将这一行放入其中:

ErrorDocument 404 /index.php

现在我的重写工作正常,我的 Controller 和我的所有页面都能正确加载。但是,我仍然收到在 firebug 中找不到的 404 页面,但我的页面仍在加载。

这里发生了一些我不理解或不尊重的奇怪而疯狂的事情。双重重定向? Apache 不知道 DocumentRoot 是什么意思?除非我在/var/www 中有另一个 .htaccess 文件,否则为什么无法识别/var/www/html/.htaccess 文件?

最佳答案

找到问题了。

在httpd.conf我改了

<Directory "/var/www/html">
        AllowOverride none

<Directory "/var/www/html">
        AllowOverride All

这很难发现,因为在该目录设置中有大量评论并且它模糊了它。 .conf 文件中还有许多其他 AllowOverride All 语句,这让我觉得我已经设置了设置。

等等,还有更多!

另外,我从 IRC 上一个名叫“thumbs”的人那里了解到,我应该将我的 .htaccess 文件的内容直接放入 httpd.conf 的 设置中。当您这样做时,您可以改回 AllowOverride none 并获得显着的性能提升。

关于apache - .htaccess 未从 DocumentRoot 目录中正确读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15231722/

相关文章:

apache - 无效命令 'RedirectMatch' - Apache 2.47。 Ubuntu 信任

php fopen 无法在 Mac OS X 中打开文件进行写入

.htaccess - htaccess RewriteRule 有效,但浏览器不显示友好的 URL

php - 多次设置 cookie header

php - Codeigniter - if 语句中的正则表达式?

django - mod_wsgi apache2 加载失败

apache - 通配符 Tomcat url 映射或过滤器映射

php - .htaccess 中的 RewriteRules 会影响站点的速度吗?

php - MVC 网址重定向

php - 如何使用数组和条件获取表值