In this tutorial I am going to show how to remove index.php from URL using .htaccess file in CodeIgniter. htaccess is the shortened used for Hypertext Access, which is a powerful configuration file that controls the directory “.htaccess”. It is used by Apache based web servers to control various server features.
Now The Question Arises Why Exactly do we want to remove index.php..?? As CodeIgniter’s URLs are designed to be search engine friendly and to human too. And so to maintain the standards we need to remove the default index.php which appears in the url of codeigniter applications, so that the url becomes search engine friendly and looks clean.
Download The Script Given Below
The script contains the code with proper documentation to perform the following transformation.
Steps To Remove index.php using .htaccess:-
Step:-1 Open the file config.php located in application/config path. Find and Replace the below code in config.php file.
// Find the below code
$config['index_page'] = "index.php"
// Remove index.php
$config['index_page'] = ""
Step:-2 Go to your CodeIgniter folder and create .htaccess file.
Path:
Your_website_folder/
application/
assets/
system/
user_guide/
.htaccess <--------- this file
index.php
license.txt
Step:-3 Write below code in .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Step:-4 In some cases the default setting for uri_protocol does not work properly. To solve this issue just open the file config.php located in application/config and then find and replace the code as:
// Find the below code
$config['uri_protocol'] = "AUTO"
// Replace it as
$config['uri_protocol'] = "REQUEST_URI"
Conclusion:
Hope these steps helped you to remove index.php in CodeIgniter framework using .htaccess. Keep reading our blogs. 🙂
35 Replies to “CodeIgniter Remove index.php By .htaccess”
i have already do it and yes it run way, but when i click some href itu return index.php again
look this url still have index.php
http://localhost/project/index.php/kategori
if it cant work so do one thing when you paste following code in .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L] …..
now correct it like this :
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]
………..
give ur route folder name in last line before index.php just like i gave (/ci/index.php) or what ever ur route foldet name ……thanks
good it works ….!
this type the file content
.htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L]
RewriteRule ^(.*)$ index.php/$1 [NC,L]
->>>>application/config/config .php change
$config[‘index_page’] = ”;
$config[‘base_url’] = ”;
$config[‘uri_protocol’] = “REQUEST_URI”;
so enjoy remove the index.php file into codeigniter
Thank you finally a solution that works.
Thaaaanks!!!!!
Hi , I have changed as you posted here but i am geting an error
“No input file specified.” in codeigniter , Please help
Hello Sanju, If file not present then you can create file name .htaccess in the root directory and your problem get resolved.
i created a .htacces file in the root but its still the same problem for me, im getting en error “internal server Error” plz help
Thanks !!! its working.
thanks………..it works for me..
Thanks it works to remove index.php
Thanks It works for me.
it’s looks like working, “index.php” was gone.. but when i click on the url, it’ll be like this..
Not Found
The requested URL /Register was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Hii,
I have created .htaccess file on root and remove index.php page from $config[‘index_page’] = “” but when i run site it is showing 500 Internal Server Error please leave your reply on this… Thanks in advance.
For this website not working htaccess
can u please provide me solution on that
existing code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Thanks in advance
This code can’t work on server..please suggest any other solution.
hiii
please have look on below code..
.htaccess code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
also change in config file:
$config[‘index_page’] = ”;
$config[‘uri_protocol’] = ‘REQUEST_URI’;
and mod_rewrite module also enabled.
although this code not working.
Thanks in advance.
Hi Rubby,
Create an .htaccess file in the root folder (same folder where application folder exist).
Now, write the following code in it:-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Note: make sure that in config.php file,
$config[‘index_page’] = ”;
$config[‘uri_protocol’] = ‘REQUEST_URI’;
Also, if you are working in a wamp server then let me tell you that in wamp server it does not work because rewrite_module by default disabled so we have need to enable it. For this do the following:-
1) Left click WAMP icon
2) Apache
3) Apache Modules
4) Left click rewrite_module
Then, Restart All Services.
Thanks & Regards,
FormGet.com
Not working with fedora 22.. do i need to change any additional values..?
This is great.
Thank you very much.
Thank you for this solution. most helpful one 🙂
Hi sir I want to generate htaccess file for codeigniter even I generated but where to keep I don’t know please can help me some one ..
Thanks. It works. But two variantes is works. If I go to http://app.bagau.dev/index.php/auth/login and if I go to http://app.bagau.ru/auth/login, both is works.
How I do that http://app.bagau.dev/index.php/auth/login redirecting to http://app.bagau.ru/auth/login ?
I found answer to my question here http://stackoverflow.com/questions/16737120/how-to-redirect-and-remove-index-php-in-codeigniter
I have tried your steps but its not working for me. Can you please guide me ?
Hello Bhaskar,
Create an .htaccess file in the root folder (same folder where application folder exist).
Now, write the following code in it:-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Note: make sure that in config.php file,
$config[‘index_page’] = ”;
$config[‘uri_protocol’] = ‘REQUEST_URI’;
Also, if you are working in a wamp server then let me tell you that in wamp server it does not work because rewrite_module by default disabled so we have need to enable it. For this do the following:-
1) Left click WAMP icon
2) Apache
3) Apache Modules
4) Left click rewrite_module
Then, Restart All Services.
Thanks & Regards,
FormGet.com
Same issue is happening again and again with me brother.
When I go to http://localhost/codeigniter/index.php/ci_sample_controller/hello,its working fine.But I remove index.php from it like http://localhost/codeigniter/ci_sample_controller/hello.It give me error
“The requested URL /codeigniter/ci_sample_controller was not found on this server.
Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80”
I just want to say thanks to you. It works for me. Much thanks!
very easy code
Hi All,
I hope all is well.
if you want to remove index.php from url first chick mod_rewrite in phpinfo();
then use .htaccess below code
RewriteEngine On
RewriteBase /
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|images|css|public)
RewriteRule ^(.*)$ /index.php/$1 [L]
Then
// Find the below code
$config[‘index_page’] = “index.php”
// Remove index.php
$config[‘index_page’] = “”
And Last
// Find the below code
$config[‘uri_protocol’] = “AUTO”
// Replace it as
$config[‘uri_protocol’] = “REQUEST_URI”
Thanks
you’re doing a good job man thanks
This is work on local host but when upload the project .htdocs file can not upload why?
Hey, Thank You so much bro… I tried every forum every site for that but nothing works on my linux machine but this is working fine.
This woks with localhost. But when i host this in azure server it becomes error like this.
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
How to solve this?