简体   繁体   中英

Remove index.php from CI in subdirectory

I saw a lot of questions like mine on stackoverflow but all of solutions doesn't work for me.

I have domain like example.com and codeigniter application - example.com/codeigniter.

I don't have any .htaccess in main directory but I want to remove index.php from my codeigniter app.

config.php :

$config['base_url'] = 'http://example.com/codeigniter';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

I've tried:

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

but I have error:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, postmaster@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Next solution is not working too:

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

because when I try to call my controller like www.example/codeigniter/user/login I have error:

Not Found

The requested URL /codeigniter/user/login was not found on this server.

Keep default settings in config.php

Try htaccess code:

In my Ubuntu Server following worked for me:

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

Also make sure that rewrite module is enabled on server. You can check using following code :

print_r(apache_get_modules());

Check these differences:

In APPPATH.'config/config.php' :

$config['base_url'] = 'http://example.com/codeigniter/';

In .htaccess :

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

Differences in my answer are mainly using slashes. To function on regular way CI requires base_url() ends with slash. Apache .htaccess file need to be told location that starts from root which is denoted with starting slash. ending slash is telling Apache that any appendix should start with no slash. This would be convention like when is in root directory RewriteBase is set just with slash or better said it is ending with slash. Try this and if more errors tell in comment because different servers can interpret .htaccess code differently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM