Codeigniter: how to remove index.php from URL?

2019-06-14 01:23发布

问题:

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';