简体   繁体   中英

Remove index.php in Codeigniter URL

How to remove index.php in Codeigniter url?

http://localhost/Sample/index.php

to just

http://localhost/Sample

I've been searching whole day in the internet and nothing works. I'm using Apache2.2 and the mod_rewrite in httpd.conf have already been enabled. The version i'm using is Codeigniter 2.1.4.

Please teach my step by step. I'm still very new in Codeigniter Framework. Thanks !

You can use the below code in your .htaccess file :

RewriteEngine On
RewriteBase /my_project_name/
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

There are two places that you must make this change, one is in your /application/config/config.php :

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';

Remove the index.php from index page.

The 2nd place is (as mentioned) in updating/creating your .htaccess file, that sits in the same root folder as your primary index.php file.

Referer to the CI guide: http://ellislab.com/codeigniter/user-guide/general/urls.html

It's all in the guide...

Try this on your .htaccess file found in the root folder:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
You just need to create .htaccess file in project folder and write:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

 # Rewrite all other URLs to index.php/URL 
 RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 

 </IfModule>
  <IfModule !mod_rewrite.c>
       ErrorDocument 404 index.php
   </IfModule>

   #And You don't need to define in base_url in config file:

         $config['base_url'] = '';// blank it.

  I hope it will work perfectly ...

First Enable "mod_rewrite" in Apache server. Restart your Apache server.

Add this code to .htaccess (Folder structure is Project_Name/.htaccess)

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

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