[英]CodeIgniter removing index.php not working
我将 Ubuntu 13 与以下设置用于本地 codeigniter 站点。
Apache/2.4.6 (Ubuntu)
5.5.3-1ubuntu2.2
'CI_VERSION', '2.1.2'
如果没有index.php
,URL 将不再工作。 它们曾经可以工作,但是在从Ubuntu 12.x
升级到13.x
并在过去一年中进行了一些 apache 更新后, localhost
站点不再正常工作。
如果我去localhost/index.php/controllername/
它工作但如果我去 localhost/controllername/ 它不。
mod_rewrite
已启用。
CodeIgniter 配置有:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; // tried all available options here and
没有任何效果
在域的.conf
文件中,我有这个:
<Directory />
Options -Multiviews +FollowSymLinks
AllowOverride All
</Directory>
这里的.htaccess
文件注释行是我尝试过但不起作用的行。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# RewriteRule .* index.php/$0 [PT,L]
# RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
我在谷歌上搜索并阅读了我能找到的所有内容,并尝试了我能找到的所有内容,包括 Stack Overflow 上的几篇文章,包括“可能已经有答案的问题”中的文章。 似乎仍然没有任何效果。 但就像我说的,这在过去有效,但只有在对操作系统和 Apache 进行多次更新后,我才第一次注意到它停止工作。
我将在未来的项目中远离 CodeIgniter,但这些项目已经存在。 对可能是什么问题感到困惑。
解决方案:
事实证明,这根本不是 codeigniter 问题。 这是一个 apache 问题,但与重写规则无关。 在我的 apache2.conf 中,我必须更改 /var/www/ 的块
要求所有授予似乎都成功了。
DirectoryIndex index.php index.html Options Indexes FollowSymLinks AllowOverride All Require all granting
只是为了更好的衡量,我也在这里进行了更改:Options FollowSymLinks AllowOverride All Require all allowed
在 askubuntu 上找到https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working
将以下代码复制到根文件夹中的.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Options All -Indexes
这对我来说很有效,可以删除 CodeIgniter 中的 index.php。
CodeIgniter 似乎没有默认的.htaccess
,所以不清楚它来自哪里。 但出于调试目的,我建议您执行以下操作。 首先,这是您的.htaccess
全部清理完毕:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
现在用这个替换你的index.php
。 它只是转储通过.htaccess
传递的$_GET
值,如下所示:
echo '<pre>';
print_r($_GET);
echo '</pre>';
现在,在您的.htaccess
加载相关站点/应用程序/项目的索引页面。 输出应该是这样的:
Array
(
[/controllername/here/] =>
)
这似乎是正确的。 但同样,你会比我们更了解。
这样做的目的是评估问题是否在于 Apache 将正确的$_GET
值传递给您的 CodeIgniter 设置,这是一个问题。 或者问题是否在您的 CodeIgniter 控制器逻辑本身内。
我的直觉告诉我,问题是在你的代码库的笨逻辑,因为从升级Ubuntu 12.x
到Ubuntu 13.x
包括升级版的PHP
从版本5.3
中Ubuntu 12.x
到版本5.5
中Ubuntu 13.x
。 我使用了许多在PHP 5.3
和PHP 5.4
中运行的代码,但在PHP 5.5
中有时会中断,因为该代码库中有重大更改,如果您不继续使用未折旧的函数等会导致代码中断.
我会尝试类似的东西:
RewriteRule ^(?!index)(.*)$ index.php?/myvariable=$1 [L]
如果您知道脚本在localhost/controllername
对controllername
期望的 GET 变量,则在规则myvariable
替换myvariable
变量名。
将 .htaccess 文件放在项目文件夹中,如下所示:
htdocs/your_project/.htaccess
试试这个 .htaccess 的新代码:
RewriteEngine on
RewriteBase /your-project-directory-name/
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.