【问题标题】:Remove index.php in Codeigniter URL删除 Codeigniter URL 中的 index.php
【发布时间】:2018-08-30 19:32:03
【问题描述】:

如何删除 Codeigniter url 中的 index.php?

http://localhost/Sample/index.php

只是

http://localhost/Sample

我在互联网上搜索了一整天,但没有任何效果。我正在使用 Apache2.2,并且 httpd.conf 中的 mod_rewrite 已经启用。我使用的版本是 Codeigniter 2.1.4。

请一步一步教我。我对 Codeigniter 框架还是很陌生。谢谢!

【问题讨论】:

  • 我们需要先查看您是否尝试了解决方案。你能先给我们看一些示例代码吗?

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

您可以在 .htaccess 文件中使用以下代码:

RewriteEngine On
RewriteBase /my_project_name/
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

【讨论】:

    【解决方案2】:

    您必须在两个地方进行此更改,一个是在您的/application/config/config.php:

    /*
    |--------------------------------------------------------------------------
    | Index File
    |--------------------------------------------------------------------------
    |
    | Typically this will be your index.php file, unless you've renamed it to
    | something else. If you are using mod_rewrite to remove the page set this
    | variable so that it is blank.
    |
    */
    $config['index_page'] = '';
    

    从索引页面中删除index.php。

    第二个地方是(如前所述)更新/创建您的 .htaccess 文件,该文件与您的主要 index.php 文件位于同一根文件夹中。

    请参阅 CI 指南: http://ellislab.com/codeigniter/user-guide/general/urls.html

    这一切都在指南中......

    【讨论】:

      【解决方案3】:

      在根文件夹中找到的 .htaccess 文件上试试这个:

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

      【讨论】:

        【解决方案4】:
        You just need to create .htaccess file in project folder and write:
        
        RewriteEngine On
        
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        
         # Rewrite all other URLs to index.php/URL 
         RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 
        
         </IfModule>
          <IfModule !mod_rewrite.c>
               ErrorDocument 404 index.php
           </IfModule>
        
           #And You don't need to define in base_url in config file:
        
                 $config['base_url'] = '';// blank it.
        
          I hope it will work perfectly ...
        

        【讨论】:

          【解决方案5】:

          首先在 Apache 服务器中启用“mod_rewrite”。 重新启动您的 Apache 服务器。

          将此代码添加到 .htaccess(文件夹结构为 Project_Name/.htaccess)

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

          【讨论】:

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