【问题标题】:CodeIgniter removing index.php not workingCodeIgniter 删除 index.php 不起作用
【发布时间】:2014-08-12 23:34:50
【问题描述】:

我正在为本地 codeigniter 站点使用具有以下设置的 Ubuntu 13。

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>

我已经在 Google 上搜索并阅读了我能找到的所有内容,并尝试了我能找到的所有内容,包括 Stack Overflow 上的几篇帖子,包括“可能已经有你答案的问题”中的帖子。似乎仍然没有任何效果。但就像我说的,这在过去有效,但只有在多次更新操作系统和 Apache 之后,我才第一次注意到它停止工作。

我将在未来的项目中远离 CodeIgniter,但这些项目已经存在。对可能是什么问题感到困惑。

解决方案:

结果证明这根本不是代码点火器问题。这是一个 apache 问题,但与重写规则无关。 在我的 apache2.conf 中,我不得不更改 /var/www/ 的块

要求所有授予似乎已经成功了。

DirectoryIndex index.php index.html 选项索引 FollowSymLinks 允许覆盖所有 要求所有授予

为了更好的衡量,我也在这里进行了更改: 选项 FollowSymLinks 允许覆盖所有 要求所有授予

在 askubuntu 上找到 https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working

【问题讨论】:

  • 您确定mod_rewrite 已启用吗?它不是默认的:sudo a2enmod rewrite。然后sudo service apache2 restart.
  • 您是否将变量设置为空白?在配置文件中? $config['index_page'] = 'index.php';应该是这样 $config['index_page'] = '';
  • 看看这是一个重复的问题stackoverflow.com/questions/20814773/…
  • 如前所述,我看到了很多关于 SO 的问题/答案,但没有一个对我的情况有所帮助。该问题中的重写规则正是我所拥有但不起作用的。事实证明,这根本不是 codeigniter 的问题,而是 apache 配置的问题。我必须在 下的 apache2.conf 中进行一些更改

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

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 控制器逻辑本身。

我的直觉告诉我问题出在代码库中的 CodeIgniter 逻辑中,因为从 Ubuntu 12.x 升级到 Ubuntu 13.x 包括升级版本的 PHPUbuntu 12.x 中的版本 5.3 到版本 5.5Ubuntu 13.x。我正在使用很多在PHP 5.3PHP 5.4 中工作的代码,但在PHP 5.5 中有时会中断,因为该代码库中存在重大变化,如果您不保持在非折旧的顶部,则会导致代码中断功能等。

【讨论】:

    【解决方案2】:

    我会尝试类似:

    RewriteRule ^(?!index)(.*)$ index.php?/myvariable=$1 [L]
    

    如果您知道脚本在 localhost/controllername 中为 controllername 期待什么 GET 变量,则在规则中将 myvariable 替换为该变量名称。

    【讨论】:

      【解决方案3】:

      将以下代码复制到根文件夹中的.htaccess

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule .* index.php/$0 [PT,L] 
      
      Options All -Indexes
      

      这对我来说可以很好地删除 CodeIgniter 中的 index.php。

      【讨论】:

        【解决方案4】:

        将 .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] 
        

        【讨论】:

          猜你喜欢
          • 2013-12-29
          • 2019-01-14
          • 2020-02-10
          • 2021-04-11
          • 1970-01-01
          • 2018-07-12
          • 1970-01-01
          • 2011-08-03
          • 2014-08-19
          相关资源
          最近更新 更多