【问题标题】:CodeIgniter removing index.php from urlCodeIgniter 从 url 中删除 index.php
【发布时间】:2016-05-14 22:44:40
【问题描述】:

我知道这个问题已经被问过很多次了。 但是,我仍然无法摆脱 index.php 网址。它阻止我直接访问我的控制器。

我目前正在使用CodeIgniter 2.2.2
以下是我的.htaccess 文件:

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

应用程序/配置:

//  Find the below code

$config['uri_protocol'] = "AUTO"

//  Replace it as

$config['uri_protocol'] = "REQUEST_URI" 

有什么想法吗?提前感谢您的宝贵时间。

【问题讨论】:

  • 你使用的是 wamp 还是 xampp?
  • 我现在正在使用 wamp,但目前我也希望能够在生产环境中访问。 (产品当前正在运行 linux apache)

标签: php .htaccess codeigniter config


【解决方案1】:

终于解决了我的问题。我想把我的答案分享给大家。 我目前正在使用 mac,我将 .htaccess 文件重命名为 htaccess.txt 并在 htaccess 中有以下代码: 重写引擎开启

#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 %{ENV:REDIRECT_STATUS} ^$
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

然后通过执行以下操作确保 Apache 正在读取 htaccess.txt 文件:

  1. 在您的 httpd.conf 中取消注释以下行

  2. 进入 extra/httpd-default.conf 文件并更改这行代码:AccessFileName .htaccessAccessFileName htaccess.txt

  3. 重启 Apache 服务器

  4. 在您的配置中,确保将“base_url”留空(索引页面仍然可以存在,没关系)。还有 $config['uri_protocol'] = 'REQUEST_URI';

希望这可以帮助其他人!

【讨论】:

    【解决方案2】:

    来自CI User Manual

    Index.php 文件将默认包含在您的 URL 中。

    示例:

    example.com/index.php/controller
    

    你可以如何改变?

    您可以使用 .htaccess 中的重写规则从 URL 中删除 index.php

    来自User Guide的示例:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    使用“否定”方法,除了指定的项目外,所有内容都被重定向。

    最后一件事让您使用 index_page 作为空位置 application/config/config.php:

    $config['index_page'] = '';
    

    最后,如果您仍然面临问题,请将 uri_protocol AUTO 更改为 REQUEST_URI

    $config['uri_protocol'] = "REQUEST_URI";
    

    【讨论】:

    • @wolfgang1983 知道了,兄弟
    • 我以前看过这个指南,没有帮助。
    • @cs-learning-101 也改变 $config['uri_protocol'] = "REQUEST_URI";如果它是自动的
    【解决方案3】:

    在您的 .htaccess 上

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

    $config['index_page'] = '';

    为我工作

    【讨论】:

      【解决方案4】:

      如果您使用 wamp,请确保您启用了 apache mod rewrite。

      这个 htaccess 适用于 wamp 或 xampp

      Options +FollowSymLinks
      Options -Indexes
      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]
      

      确保将其放在主目录中。

      $config['index_page'] = '';
      

      【讨论】:

        【解决方案5】:

        这对我有用。

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

        【讨论】:

          【解决方案6】:

          我之前也遇到过同样的问题。这就是我所做的

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

          然后转到 config.php 并删除 index.php

          $config['index_page'] = 'index.php';
          //change it to
          $config['index_page'] = '';
          

          【讨论】:

            【解决方案7】:

            试试这个..

            RewriteEngine on
            RewriteBase /
            # Hide the application and system directories by redirecting the request to index.php
            RewriteRule ^(application|system|\.svn) index.php/$1 [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php/$1 [QSA,L]
            

            【讨论】:

              【解决方案8】:

              在您的 .htacess 中使用以下代码

                 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]
              

              【讨论】:

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