【问题标题】:Cannot remove index.php from CodeIgniter URL无法从 CodeIgniter URL 中删除 index.php
【发布时间】:2012-12-27 04:37:15
【问题描述】:

我的目录结构是 /var/www/CI/,所有文件夹,即应用程序,系统,在 CI 文件夹下。在 CI 下创建了一个 .htaccess 文件。

以下是.htacess中的代码。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

仍然 localhost/CI/blog 给出 404 错误。谁能指导一下这条规则哪里错了?

【问题讨论】:

    标签: .htaccess codeigniter codeigniter-2


    【解决方案1】:

    请尝试以下方法

    首先,请确保您将配置文件设置为如下所示..

    $config['index_page'] = '';
    

    还要确保在 httpd.conf 文件中启用了 mod_rewrite,然后,使用以下代码覆盖位于项目根文件夹而不是应用程序文件夹中的 .htaccess 文件。

    RewriteEngine on
    RewriteCond $1 !^(index\.php|public|\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1
    

    【讨论】:

    • 我已经收到了几个博客和帖子来寻找这个答案,你是唯一一个对我有正确答案并且非常清楚该怎么做的人。非常感谢!
    • 感谢@Travis Michael Heller
    • 感谢@Arnab vai.it 帮助我
    • 我忘记添加 $config['index_page'] = ' ';
    • 感谢您提及“项目根文件夹”。我一直在努力弄清楚这个 .htaccess 文件应该放在哪里。不是只有一个。
    【解决方案2】:

    确保在您的application/config/config.php 文件中设置如下:

    $config['index_page'] = '';
    

    也这样做:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^CI/(index\.php|public|images|robots\.txt|css)
        RewriteRule ^CI/(.*)$ CI/index.php/$1 [L]
    </IfModule>
    

    通常您不会将 CI 放在自己的目录中,而是将 applicationsystem 放在根文件夹中。

    【讨论】:

    • 执行了上述操作但仍然无法正常工作。还有其他地方需要配置吗?
    【解决方案3】:

    检查您的 Apache 配置,使其允许覆盖。 如果 AllowOverride 设置为 None,则重写将不适合您。

    将以下内容添加到您的 httpd.conf(不是您的 .htaccess)应该可以解决您的问题

    <Directory "/">
        AllowOverride All
    </Directory>
    

    如果它仍然不起作用,请告诉我。

    【讨论】:

    • 如果我错了,请纠正我,但不应该是 httpd.conf 吗?我的 httpd.conf 文件也是空的。
    • 感谢您抽出宝贵时间回答我的问题。我很感激。
    • 对不起,我的错误......它是httpd.conf!我确定了我的答案。我希望这个解决方案有所帮助。
    【解决方案4】:

    这行得通。虽然,请确保您的 mod_rewrite 已启用,

    1. 在 Apache 的安装文件夹内的 C:\wamp\bin\apache\apache2.4.9\conf 文件夹下找到 httpd.conf 文件。
    2. httpd.conf 文件中找到以下行#LoadModule rewrite_module modules/mod_rewrite.so。您可以通过搜索关键字“mod_rewrite”轻松完成此操作。
    3. 去掉行首的##表示该行已被注释。
    4. 然后重启apache服务器
    5. 您现在可以在执行phpinfo() 时在加载的模块部分看到mod_rewrite

    【讨论】:

      【解决方案5】:

      使用以下步骤,
      1. 在文档根目录 [myroot_folder/.htaccess] 中创建 .htaccess 文件。
      2.打开application/config/config.php中的配置文件,执行以下操作

      $config['index_page'] = '';
      

      从您的基本网址中删除 index.php
      如果是$config['base_url']= 'http://localhost/myroot_folder/index.php';
      将其更改为$config['base_url']= 'http://localhost/myroot_folder/';

      现在打开.htaccess 文件并添加以下代码块

      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]
      

      【讨论】:

        【解决方案6】:

        1) 制作.htaccess 文件并放入此代码。

          RewriteEngine on
          RewriteCond $1 !^(index\.php|resources|robots\.txt)
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
        

        2) 改变:

         $config['index_page'] = ''; 
         remove index.php present in config.php file
        

        3) 从你的服务器重写

        【讨论】:

          【解决方案7】:

          如果你还没有完成所有工作,但你已经完成了,试试这个:

          /etc/apache2/sites-available/default 中的虚拟主机文件中将 AllowOverride None 更改为 AllowOverride All

          Details here

          【讨论】:

            猜你喜欢
            • 2013-08-09
            • 1970-01-01
            • 1970-01-01
            • 2023-03-25
            • 2015-05-26
            • 1970-01-01
            • 2012-11-14
            • 2016-05-14
            • 2014-10-28
            相关资源
            最近更新 更多