【问题标题】:Codeigniter: how to remove index.php from URL?Codeigniter:如何从 URL 中删除 index.php?
【发布时间】:2015-10-24 14:04:43
【问题描述】:

我查看了很多文章并尝试了不同的方法,但仍然无法正常工作。

现在我的 URL 看起来像 mysite.com/index.php/[controller]。 目标是mysite.com/[controller]

config.php 中:

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

我的 .htaccess 文件包含

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

在我的 Apache 服务器中也启用了 mod_rewrite

还有其他解决方案吗?

【问题讨论】:

  • 你使用的是linux还是windows操作系统
  • 勾选this一个。

标签: php codeigniter url-rewriting codeigniter-3


【解决方案1】:

在你的配置文件中试试这个

$config['base_url'] = '';
$config['server_root'] = $_SERVER['DOCUMENT_ROOT'];

【讨论】:

    【解决方案2】:

    这个对我来说很好用,你可以试试这个

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

    【讨论】:

      【解决方案3】:

      这对我有用 [在 linux 操作系统]:

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

      【讨论】:

        【解决方案4】:

        在配置文件中使用 base_url 中的项目 url

        $config['base_url'] = 'http://localhost/project';
        

        【讨论】:

          【解决方案5】:

          application/config/config.php

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

          .htaccess(放置在应用程序文件夹之外)

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

          2018 年 2 月 27 日更新

          $config['base_url'] = 'https://stackoverflow.com/'; # Must fill
          

          否则您会收到此错误Codeigniter echoing [::1] instead of localhost

          使用这个.htaccess

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

          Refference

          【讨论】:

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