【问题标题】:Issues in removing index.php in CodeIgniter 3在 CodeIgniter 3 中删除 index.php 的问题
【发布时间】:2015-08-10 16:16:03
【问题描述】:

我想在 CodeIgniter 中访问没有index.php 的 URL。这是我的Blog 控制器

class Blog extends CI_Controller {

    public function index() {
        echo 'hello world';
    }

    public function about() {
        echo 'about page';
    }
}

现在我可以通过http://localhost/codeigniter/index.php/Blog 访问index,但我需要通过这个URL http://localhost/codeigniter/Blog 访问它。

注意

我从配置文件中删除了 index.php .htaccess文件

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

Environment

Windows、XAMPP、PHP 版本 5.6.3

【问题讨论】:

  • google 一下,“codeginiter 删除 index.php 文件”。
  • 编辑您的.htaccess 文件,点击here 了解如何操作。
  • 我试过但没用
  • 做 2 件事,确保您已将 apache 配置为使用 .htaccess 并启用了 mod_rewrite.c 模块。
  • 我已经编辑了我的问题

标签: php codeigniter


【解决方案1】:

我的 Codeigniter 版本是 3.1.11 我在 htaccess 中尝试了所有类型的代码,但是这不起作用然后我找到了这个代码。

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

【讨论】:

    【解决方案2】:

    即使在使用 Codeigniter 2 年多之后,我现在又在这部分苦苦挣扎。

    没有一个答案对我有确切的帮助,包括official Codeigniter's page 关于这个,所以我发布了对我有用的解决方案。

    1. 启用重写引擎

      sudo a2enmod rewrite
      
    2. 更改 Apache 的配置文件以允许文件夹允许覆盖默认安全设置

      sudo nano /etc/apache2/apache2.conf
      

    根据文件的位置,在“目录”选项下进行编辑

        AllowOverride None
    

        AllowOverride All
    

    例如:我的服务器文件在“/var/www”中,所以相应目录选项的最终结果是:

        <Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
    1. 在您的 Codeigniter 项目的根目录中创建一个“.htaccess”文件,即:与应用程序、系统文件夹一起存在的位置。

    在文件中,输入以下内容:

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
    
    1. 在 config/config.php 文件中,修改:

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

        $config['index_page'] = '';
    
    1. 重启apache2:

      sudo service apache2 restart
      

    享受吧!

    【讨论】:

    • 很高兴它有帮助!
    【解决方案3】:

    要删除 index.php,请在与您网站的主 index.php 文件相同的文件夹中创建一个 .htaccess 文件。
    然后将以下代码添加到这个新创建的 .htaccess 文件中:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    
        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1 [L]
    </IfModule>
    

    这段代码有两部分
    1 - 删除 index.php
    2 - 将所有站点请求重定向到索引文件。

    【讨论】:

    • 你能检查一下你的apache是​​否启用了mod_rewrite吗?
    • ii 已检查它是否已启用
    【解决方案4】:

    好吧,我已经为此苦苦挣扎了两天,这是我针对 XAMPP 和 CodeIgniter 3 的解决方案

    在根目录下新建“.htaccess”文件(不是在CodeIgniter的应用程序文件夹中,那里还有另一个htacces文件,不要碰那个),里面有这个:

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

    然后,在 config.php(CodeIgniter 文件,位于 config 文件夹)上找到并更改此值:

    $config['base_url']     = 'localhost/YourProject';
    $config['index_page']   = '';
    $config['uri_protocol'] = 'REQUEST_URI';
    

    最后,在 httpd.conf(Apache 配置文件)上,找到:

    #LoadModule rewrite_module modules/mod_rewrite.so

    并取消注释该行(删除#)。


    就是这样!现在

    localhost/myProject/news

    就像我写的一样工作

    localhost/myProject/index.php/news
    

    【讨论】:

      【解决方案5】:

      最后这对我来说很好用。我已经用这个内容更改了.htaccess 文件

      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteBase /codeigniter
      
          #Removes access to the system folder by users.
          #Additionally this will allow you to create a System.php controller,
          #previously this would not have been possible.
          #'system' can be replaced if you have renamed your system folder.
          RewriteCond %{REQUEST_URI} ^system.*
          RewriteRule ^(.*)$ /index.php?/$1 [L]
      
          #When your application folder isn't in the system folder
          #This snippet prevents user access to the application folder
          #Submitted by: Fabdrol
          #Rename 'application' to your applications folder name.
          RewriteCond %{REQUEST_URI} ^application.*
          RewriteRule ^(.*)$ /index.php?/$1 [L]
      
          #Checks to see if the user is attempting to access a valid file,
          #such as an image or css document, if this isn't true it sends the
          #request to index.php
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?/$1 [L]
      
      
          </IfModule>
      
          <IfModule !mod_rewrite.c>
              # If we don't have mod_rewrite installed, all 404's
              # can be sent to index.php, and everything works as normal.
              # Submitted by: ElliotHaughin
      
          ErrorDocument 404 /index.php
      </IfModule>
      

      这些改动

          $config['index_page'] = "index.php" 
              //replace with the below code
              $config['index_page'] = ""
      $config['base_url'] = "your-project-url";
      

      【讨论】:

        【解决方案6】:

        在 config.php 中

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

        .htacess

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

        【讨论】:

          【解决方案7】:

          如何从 url 中删除 index.php 已经被问过很多次了,它与 codeigniter 2 和 3 的相同。

          对于带有 Codeigniter Windows 的 Xampp

          找到应用程序/config/config.php

          替换这个

          $config['base_url'] = "";

          有了这个

          $config['base_url'] = "your-project-url";

          替换这个

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

          有了这个

          $config['index_page'] = ""

          在主目录中创建名为 .htaccess 的文件

          我在 Windows 中的 xampp 中使用下面的代码对我来说效果很好。更多htacces here

          Options +FollowSymLinks
          Options -Indexes
          
          <FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
              Order deny,allow
              Deny from all
          </FilesMatch>
          
          DirectoryIndex index.php
          RewriteEngine on
          RewriteCond $1 !^(index\.php|images|robots\.txt)
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php/$1 [L,QSA]
          

          注意:确保您的控制器类似于示例 Welcome.php 而不是 welcome.php 如果删除 index.php,您可能需要在 route.php 中创建新路由

          【讨论】:

            【解决方案8】:

            步骤:-1 打开文件夹“application/config” 并打开文件“config.php“。在config.php文件中查找并替换以下代码。

             //find the below code   
                $config['index_page'] = "index.php" 
                //replace with the below code
                $config['index_page'] = ""
            

            步骤:-2 在 .htaccess 文件中写入以下代码

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

            步骤:-3 在某些情况下,uri_protocol 的默认设置无法正常工作。要解决这个问题,只需打开文件“application/config/config.php“,然后找到并替换以下代码

            //find the below code
            $config['uri_protocol'] = "AUTO"
            //replace with the below code
            $config['uri_protocol'] = "REQUEST_URI" 
            

            【讨论】:

              【解决方案9】:

              启用 mod_rewrite 重启你的 apache 服务器 并在您的 .htaccess 中

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

              【讨论】:

                【解决方案10】:

                请更改 application/config 中存在的 base_url 路径

                $config['base_url'] ='localhost/projectname/index.php' 
                

                $config['base_url'] ='localhost/projectname/' 
                

                【讨论】:

                  猜你喜欢
                  • 2013-12-08
                  • 2022-01-06
                  • 2015-09-28
                  • 2020-07-08
                  • 2016-11-23
                  • 2017-09-14
                  • 2021-12-15
                  • 2016-01-16
                  • 1970-01-01
                  相关资源
                  最近更新 更多