Codeigniter: how to remove index.php from URL?

帅比萌擦擦* 提交于 2019-12-10 11:56:55

问题


I've looked at many articles and tried different ways but it still doesn't work.

Now my URLs look like mysite.com/index.php/[controller]. The goal is mysite.com/[controller].

In config.php:

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

My .htaccess file contains

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

Also mod_rewrite is enabled in my Apache server.

Any other solutions?


回答1:


In application/config/config.php

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

in .htaccess (Place Outside application folder)

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

Update 2018-02-27

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

else you get this error Codeigniter echoing [::1] instead of localhost

use this .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




回答2:


try this in your config file

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



回答3:


This one is working fine for me, you can try with this

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]



回答4:


This works for me [in linux operating system]:

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



回答5:


in config file use your project url in base_url

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


来源:https://stackoverflow.com/questions/31770799/codeigniter-how-to-remove-index-php-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!