【问题标题】:CodeIgniter Remove index.php in URLCodeIgniter 删除 URL 中的 index.php
【发布时间】:2014-10-29 07:56:19
【问题描述】:

我在Ubuntu系统中创建了一个codeigniter项目,并在base_url后面添加如下。

http://192.168.1.123/project/

如果我访问任何其他网址,如下所示。

http://192.168.1.123/project/course/view/1

重定向到“找不到页面”。如果我在base_url 之后包含index.php,它工作正常。

我创建了.htaccess 文件如下。

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

在config.php 文件中

$config['index_page'] = 'index.php';

改为

$config['index_page'] = '';

但它仍然无法正常工作。请建议灵魂。工作将不胜感激。

【问题讨论】:

标签: .htaccess codeigniter codeigniter-2 codeigniter-url


【解决方案1】:

更改 .htaccess 文件并尝试一下

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

【讨论】:

    【解决方案2】:

    第 1 步:

    在 htaccess 文件中添加这个

    <IfModule mod_rewrite.c>
      RewriteEngine On
      #RewriteBase /
    
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^ index.php [QSA,L]
    </IfModule>
    

    第 2 步:

    删除 codeigniter 配置中的 index.php

    $config['base_url'] = ''; 
    $config['index_page'] = '';
    

    第 3 步:

    允许在 Apache 配置(命令)中覆盖 htaccess

    sudo nano /etc/apache2/apache2.conf
    

    并编辑文件并更改为

    AllowOverride All
    

    对于 www 文件夹

    第 4 步:

    启用 apache mod rewrite(命令)

    sudo a2enmod rewrite
    

    第 5 步:

    重启 Apache(命令)

    sudo /etc/init.d/apache2 restart
    

    【讨论】:

      【解决方案3】:

      试试这个:

      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteBase /project/
          RewriteRule ^index\.php$ - [L]
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule . /project/index.php [L]
      </IfModule>
      

      并且,在config.php:

      $config['index_page'] = 'index.php';
      

      请确保mod_rewrite 模块已启用

      【讨论】:

        【解决方案4】:

        首先将.htaccess文件放到应用文件夹之外。

        然后将以下代码放入 .htaccess 文件中

        重写引擎开启

        RewriteCond %{REQUEST_FILENAME} !-f

        RewriteCond %{REQUEST_FILENAME} !-d

        重写规则 ^(.*)$ index.php/$1 [L]

        现在你可以在没有 index.php 的情况下刷新你的 url,它肯定会工作

        【讨论】:

          【解决方案5】:

          对于那些使用 Linux 的人,这是要遵循的步骤

          打开项目根文件夹中的 .htaccess 文件。这与 application 文件夹位于同一文件夹中。

          编辑.htaccess的内容到

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

          打开配置文件并修改

          $config['index_page'] = 'index.php';
          

          到

          $config['index_page'] = '';
          

          导航到 etc/apache2 并打开一个名为 apache2.conf

          的文件夹

          将 AllowOverride 从 None 编辑为 All,如下所示

          <Directory /var/www/>
              Options Indexes FollowSymLinks
              AllowOverride None
              Require all granted
          
          </Directory>
          

          到

          <Directory /var/www/>
              Options Indexes FollowSymLinks
              AllowOverride All
              Require all granted
          
          </Directory>
          

          打开终端并运行此命令

          sudo a2enmod rewrite
          

          要激活新配置,请使用此命令重新启动 apache

          systemctl restart apache2
          

          您现在已成功从您的网址中删除 index.php

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-05-14
            • 2014-10-28
            • 2012-06-15
            • 2012-09-21
            • 2011-10-12
            • 2017-07-16
            相关资源
            最近更新 更多