Remove index.php from URL in Codeigniter

2019-02-09 14:42发布

问题:

I have done this lots of time. But than also I am stuck here again (in a different server) and can not figure out what is the issue.

Completed htaccess editing

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /glt.me/CI/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /glt.me/CI/index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
  • rewrite module is enabled (as per customer care perons says)
  • Error :

    Internal Server Error

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

    Please contact the server administrator, cgiadmin@yourhostingaccount.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.

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Test this case :

http://glt.me/CI/test
http://glt.me/CI/index.php/test

回答1:

This is the simple way and it works fine for removing index.php from your url

RewriteEngine on
RewriteCond $1 !^(index\.php|uploads|css|js|lib|img|bootstrap|robots\.txt)
RewriteRule ^(.*)$ /manage/index.php/$1 [L]

instead of manage put your own application folder name.


回答2:

Use following htaccess code to remove index.php from your codeigniter url

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


回答3:

Though your client confirmed the rewrite module is working, what don't you confirm it yourself, make a php file on the root of the project with the code below and try to browse the file.

<?php
 if( !function_exists('apache_get_modules')){
        echo 'Rewrite module not available';
 }else{
    if(in_array('mod_rewrite',apache_get_modules()))
        echo 'Rewrite module enabled';
 }


回答4:

First check whether your apache is supporting mod_rewrite. If yes then add a .htaccess file. My codeigniter .htaccess file is:

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

Finally in your config file, change your base_url, remove index.php from that if you have added.



回答5:

DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteRule ^pages/.*$ http://localhost/test/? [R=301,L]


回答6:

I wanted to contribute here as I was having trouble myself. I'm using IIS5.1 for development only (Highly not recommended!)

1- Make sure that you config.php file has:

$config['index_page'] = '';

2- My app is not on the root folder, it's at localhost/CodeIgniter/. I updated config.php to:

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

3- You need Mod-Rewrite capabilities. This is embedded with most servers, but IIS5.1 needs a separate tool. I used: IIS Mod-Rewrite by micronovae. It's free as long as you use it on localhost

I put the following code (Instead of CodeIgniter, put the name of the folder):

RewriteEngine on
RewriteCond $0 !^/CodeIgniter/(css|js|swf|images|system|tools|themes|index\.php) [NC]
RewriteRule ^/CodeIgniter/(.*)$ /CodeIgniter/index.php/$1 [L]

If your application is at the root, the code you should use would be simply:

RewriteEngine on
RewriteCond $0 !^(css|js|swf|images|system|tools|themes|index\.php) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]

Hope it helps.



回答7:

Only three steps are require to remove index.php from url in Codeigniter in WAMP environment.

1) Create .htacess file in parallel to application holder and just copy past the following code:

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

2) Change $config['index_page'] to blank in config.php in application folder as below:

$config['index_page'] = '';

3) Enable “rewrite_module” of apache.

Restart your apache and its done.

Details : http://sforsuresh.in/removing-index-php-from-url-of-codeigniter-in-wamp/