【问题标题】:How to remove "index.php" in codeigniter's path如何删除codeigniter路径中的“index.php”
【发布时间】:2010-11-29 13:04:31
【问题描述】:

如何删除在中心某处的 codeigniter 的每条路径中突出的"index.php"? 我想要干净的非index.php-fied URLs

【问题讨论】:

标签: php mod-rewrite codeigniter url-rewriting


【解决方案1】:

如果您使用的是 Apache,请在您的 Web 根目录中放置一个 .htaccess 文件,其中包含以下内容:

RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

另一个好版本在这里:

http://snipplr.com/view/5966/codeigniter-htaccess/

【讨论】:

  • +1 链接!虽然不得不稍微调整一下。用这个得到它 --> RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [QSA,L]
  • 出于某种原因,我不得不在 RewriteCond 的开头添加一个斜杠。 RewriteCond $1 !^\/(index\.php|assets|robots\.txt)
  • 由于某种原因,如果您的 URL 中有 index.php,它实际上并没有被删除,它会保留在那里并继续工作。
  • 我使用了所有可能的组合,但它仍然不适合我!!!!!!!你是怎么让它工作的??
  • 您的 Apache 安装需要配置为检查 .htaccess 文件 - 确保是这种情况。
【解决方案2】:

我在删除 index.php 时遇到了一些大问题。作为一般规则,下面的 .htaccess 已经在多台服务器上进行了测试并且通常可以正常工作:

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

<Files "index.php">
AcceptPathInfo On
</Files>  

如果您对此不满意,那么下一步就是调整您的配置文件。尝试其他一些 URI 协议,例如

| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO

   $config['uri_protocol']  = 'ORIG_PATH_INFO';

如果您仍然没有任何运气,请尝试更改重写规则以包含您的子文件夹。如果您在开发服务器等上使用临时 URL,这通常是一个问题:

RewriteRule ^(.*)$ /subofolder1/subfolder2/index.php/$1 [L]  

只需使用这些选项,应该可以工作。另外,请确保您的索引文件设置为:

$config['index_page'] = '';

祝你好运!

【讨论】:

  • 我认为你的第一个代码是我的解决方案,但实际上它只是将任何 url 像 domain/* 发送到 domain/index.php 它并没有发送 domain/welcome 到欢迎控制器。当时激动了一秒。现在尝试其他建议。
【解决方案3】:

在应用程序根目录中拥有 .htaccess 文件以及 index.php 文件。 (检查 htaccess 扩展是否正确,Bz htaccess.txt 对我不起作用。)

并将以下规则添加到 .htaccess 文件中,

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

然后在你的 application/config/config.php 文件中找到下面一行

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

将变量设置为空,如下所示。

$config['index_page'] = '';

就是这样,它对我有用。

如果它不起作用,请尝试用这些参数('AUTO'、'PATH_INFO'、'QUERY_STRING'、'REQUEST_URI'和'ORIG_PATH_INFO')一一替换以下变量

$config['uri_protocol'] = 'AUTO';

【讨论】:

  • 谢谢你,我已经失败了很长时间不知道为什么,但你的方法就像黄油一样工作:D
  • 感谢 MDeSilva。这篇文章节省了我的时间。
  • 谢谢。其作品。顺便说一句,我不需要更改 config.php。
  • 谢谢,这个帮助了我,我用 PATH_INFO 和 WAMP
【解决方案4】:

查看system\application\config\config.php文件,有一个变量名为index_page

应该是这样的

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

改成

$config['index_page'] = "";

然后如前所述,您还需要在.htaccess 文件中添加重写规则

注意:在 CodeIgniter v2 中,此文件已从系统文件夹中移出到 application\config\config.php

【讨论】:

    【解决方案5】:

    上述所有方法对我来说都失败了,然后我发现我没有在 /etc/apache2 的虚拟主机文件中将 AllowOverride None 更改为 AllowOverride All /sites-available/default

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
    
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride All    <---- replace None with All
        </Directory>
        <Directory /var/www >
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All   <---  replace None with All
                Order allow,deny
                allow from all
        </Directory>
    
         ...
    

    【讨论】:

    • 非常感谢,我也得到了 AllowOverride None,现在它可以工作了!
    • 博客现在已经不存在了,我非常想阅读它。这些解决方案都不适合我...
    • 太棒了,拯救了我的一天。
    【解决方案6】:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /codeigniter/index.php/$0 [PT,L]
    

    在使用项目文件夹名称“codeigniter”更改 RewriteRule .* index.php/$0 [PT,L] 之后。它为我工作。谢谢大家的支持。

    【讨论】:

      【解决方案7】:

      第 1 步 => 将您的 .htaccess 文件放在应用程序和系统文件夹所在的根文件夹中。

      第 2 步 => 如果您的网络路径使用子文件夹,例如 - yourdomain.com/project/ - 然后在 htaccess 文件中使用以下代码

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

      如果您的网络路径使用根文件夹(例如 yourdomain.com),则在 htaccess 文件中使用以下代码

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

      【讨论】:

      • 尝试使用上面的第一个示例仅适用于我的根目录,但不适用于表单加载等仍会尝试在 url 中使用 index.php 我会玩弄,如果可以的话,我会回到这个出来
      • 谢谢@Vinod。我被这个问题发疯了。我在网上搜索了很长时间,尝试了各种解决方案,但在我遇到你的解决方案之前它们从未奏效。现在我的 CI 项目是工作,它给了我继续使用 CI 的新希望。非常感谢 :)
      【解决方案8】:

      只是想我可以添加

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

      将是 .htaccess 并确保以下列方式编辑您的 application/config.php 变量:

      替换

      $config['uri_protocol'] = “AUTO” 
      

      $config['uri_protocol'] = “REQUEST_URI”
      

      【讨论】:

        【解决方案9】:
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .* index.php/$0 [PT,L] 
        

        这肯定行得通..试试看

        【讨论】:

          【解决方案10】:

          确保您已启用 mod_rewrite(我没有)。
          要启用:

          sudo a2enmod rewrite  
          

          另外,将AllowOverride None 替换为AllowOverride All

          sudo gedit /etc/apache2/sites-available/default  
          

          终于……

          sudo /etc/init.d/apache2 restart  
          

          我的 .htaccess 是

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

          【讨论】:

          【解决方案11】:

          完美的解决方案 [在本地主机和真实域上测试]

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

          【讨论】:

            【解决方案12】:

            这是我的一个 CI 项目的 .htaccess:

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

            最后一行应该提供您需要的内容,但您可能需要也可能不需要“/projectFolder”,具体取决于您的文件夹结构的设置方式。祝你好运!

            【讨论】:

              【解决方案13】:

              按照 CI wiki 中 this tutorial 中的说明使用 mod_rewrite

              【讨论】:

                【解决方案14】:

                作为 .htaccess 文件,一个不错的选择是使用Kohana Framework提供的那个:

                # Turn on URL rewriting
                RewriteEngine On
                
                # Installation directory
                RewriteBase /
                
                # Protect hidden files from being viewed
                <Files .*>
                    Order Deny,Allow
                    Deny From All
                </Files>
                
                # Protect application and system files from being viewed
                RewriteRule ^(?:application|system)\b.* index.php/$0 [L]
                
                # Allow any files or directories that exist to be displayed directly
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                
                # Rewrite all other URLs to index.php/URL
                RewriteRule .* index.php/$0 [PT]
                

                这是一个经过深思熟虑的 .htaccess 文件。

                【讨论】:

                  【解决方案15】:

                  解决codeigniter的url路径中的index.php有两种方法

                  1:在 config/config.php 更改代码:

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

                  $config['index_page'] = '';
                  

                  2:在根路径下创建.htacess,如果没有创建,复制以下代码:-

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

                  保存它,然后检查您的 Apache 配置 rewrite_module 或 mod_rewrite 是否已启用。如果没有,请启用。 (最佳方法)!

                  【讨论】:

                  • 这是正确的,直接来自 CodeIgniter 文档。我也用这个方法。
                  【解决方案16】:

                  我在 apache2 上对许多不同的主机进行了测试,效果很好。

                  使用这个htaccess

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

                  确保您有 enabled mod_rewirtephpinfo();

                  然后在 config/config.php 中执行此操作:

                  $config['index_url']    = '';
                  |
                  | 'AUTO'            Default - auto detects
                  | 'PATH_INFO'       Uses the PATH_INFO
                  | 'QUERY_STRING'    Uses the QUERY_STRING
                  | 'REQUEST_URI'     Uses the REQUEST_URI
                  | 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO
                  |
                  */
                  $config['uri_protocol'] = 'AUTO';
                  

                  如果它还不起作用,请尝试将 $config['uri_protocol']='AUTO' 更改为第 40/54 行的 application/config/config.php 文件中列出的之一:

                  有时我使用:REQUEST_URI 而不是 AUTO"QUERY_STRING" 用于 goDaddy 托管

                  【讨论】:

                    【解决方案17】:

                    查看\application\config\config.php文件,有一个变量名为index_page

                    应该是这样的

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

                    改成

                    $config['index_page'] = "";
                    

                    然后如上所述,您还需要向.htaccess 文件添加重写规则,如下所示:

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

                    它对我有用,希望你也一样。

                    【讨论】:

                      【解决方案18】:

                      如果您在 linux 上并使用 apache2 服务器,那么除了 .htaccess 文件的更改之外,我们可能还需要覆盖 apache2.conf 文件。在 /etc/apache2/apache2.conf 上找到 apache2 配置文件。

                      搜索目录 /var/www/ 更改 AllowOverride None -> AllowOverride All

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

                      【讨论】:

                        【解决方案19】:

                        在您的网络根目录中放置一个 .htaccess 文件

                        无论您做什么调整 - 如果不满足上述要求 - 它将无法正常工作。通常它在系统文件夹中,它应该在根目录中。干杯!

                        【讨论】:

                        • 花了几个小时后,我阅读了这篇文章并发现了我的错误。
                        • 因为这个“小”把戏,我也花了至少一个小时。默认情况下,.htaccess 文件位于application 目录而不是根目录中。
                        【解决方案20】:

                        我尝试了很多解决方案,但我想出的是这样的:

                        DirectoryIndex index.php
                        RewriteEngine on
                        
                        RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots  \.txt|favicon\.ico)
                        RewriteCond %{REQUEST_FILENAME} !-f
                        RewriteCond %{REQUEST_FILENAME} !-d
                        RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
                        

                        我会根据需要从 url 中删除 index.php。

                        【讨论】:

                          【解决方案21】:

                          我在.htaccess 文件中使用这些行;我放在根文件夹中

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

                          那我可以访问http://example.com/controller/function/parameter

                          而不是http://example.com/index.php/controller/function/parameter

                          【讨论】:

                            【解决方案22】:

                            这会帮助你 将此代码粘贴到 .htacess 文件中的应用程序文件夹中

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

                            【讨论】:

                              【解决方案23】:

                              这个给了我完整的魔法:

                              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]
                              

                              希望对你有帮助!

                              【讨论】:

                                【解决方案24】:

                                嗨,这个对我有用

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

                                还有这个

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

                                如果此 codeigniter 应用程序作为子域托管,则文件夹是子文件夹的名称 例如域名/文件夹/index.php。

                                希望对你有用。谢谢。

                                【讨论】:

                                  【解决方案25】:

                                  在您的 .htaccess 中复制过去的以下代码

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

                                  【讨论】:

                                  • 虽然此代码可能会回答问题,但提供有关 why 和/或 如何 此代码回答问题的附加上下文可提高其长期价值.
                                  【解决方案26】:
                                  <IfModule mod_rewrite.c>
                                  RewriteEngine On
                                  RewriteBase /Foldername of your ci/
                                  RewriteCond %{REQUEST_FILENAME} !-f
                                  RewriteCond %{REQUEST_FILENAME} !-d
                                  RewriteRule ^(.*)$ index.php/$1 [L]
                                  </IfModule>
                                  

                                  【讨论】:

                                    【解决方案27】:

                                    第 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 重写

                                    第 5 步:

                                    重启 Apache(命令)

                                    sudo /etc/init.d/apache2 restart
                                    

                                    【讨论】:

                                      猜你喜欢
                                      • 2016-08-30
                                      • 2014-07-19
                                      • 2017-08-28
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 2014-08-26
                                      • 2011-01-18
                                      • 1970-01-01
                                      相关资源
                                      最近更新 更多