While using codeigniter, I am facing some difficulties. That's about the index.php file in URLs.
CodeIgniter serves all it's pages using index.php
. So, our url's should look like following:
http://www.example.com/codeigniter/index.php/something
I want to remove the index.php
from URLs by editing the config.php
file and creating an .htaccess file. After changing the config from
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
I am able to run my project without the index.php just like this:
http://www.example.com/codeigniter/something
So now, that link is working - but it's also working with the index.php
fragment. I can also see the components with the http://www.example.com/codeigniter/index.php/something
address.
My .htaccess file is :
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Now, I want to make my site only available without the index.php
in my links. I don't want this site to run with the index.php file in the link. I want http://www.example.com/codeigniter/something
to run perfectly and http://www.example.com/codeigniter/index.php/something
not to run. How can I do this?
you can try adding the following in your .htaccess
file
RewriteCond %{THE_REQUEST} codeigniter\/index\.php
RewriteRule codeigniter\/index\.php -[F]
Basically, this rule tells to return a forbidden response if the request contains codeigniter/index.php
Add this rule above the one that you already have:
RewriteCond %{THE_REQUEST} \ /codeigniter/index\.php([^\?\ ]*)
RewriteRule ^ /codeignighter/%1 [L,R=301]
And it'll redirect requests that have index.php
in it to the URL without it.
try this one
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
put the code in your root folder htaccess.Please let me know if you face any problem
You should add a ? after index.php, so it works in all hostings. This is my htaccess
RewriteEngine On
#RewriteBase / maybe you don't need it, depends on the hosting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt|humans\.txt|license\.txt|sitemap\.xml|assets)
RewriteRule ^(.*)$ index.php?/$1 [L]