Sharing Information
Sharing Information
Picture of the author
Pankaj PawarinPHP
17th Sept 24.07:00 Min.
https://sharinginfo.in/php/remove-index-php-from-url-using-htaccess-file-in-codeigniter-30.html

Remove index.php from url using .htaccess file in codeIgniter

Remove index.php from url using .htaccess file in codeIgniter
Photo by Simon Abrams on Unsplash

We all knows we need seo freindly urls for better page ranking in search engine. search engine always try to searve good and accurate content to user which user is searching for.

when you are trying to remove some part from url, what you will exactlly do?

when you are try to remove index.php from url, you are not exactly remove them from from url, you are just hide them by using mod rewrite rules or you will say url rewriting rules.

Here is one example of url rewriting using .htaccess in codeigniter, .htaccess file you need to add or you will find in the root directory of codeigniter setup.

In CI by default, the index.php file will be included in your URLs:

example.com/<b>index.php</b>/news/article/my_article

You can easily remove this from url by using a .htaccess file with some simple rules. Here is an example of such a file, using the “negative” method in which everything is redirected except the specified items:

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

In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.

i will give here another example, if you have codeigniter setup in sub-directory under root domain then you need to set RewriteBase path.

Rewrite base path is the path of your sub dirctory from root domain to codeigniter base(root) directory.

<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/under/sub/directory/
# then use
# RewriteBase /mypage/under/sub/directory/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>

above mention two htaccess file code you will use to remove index.php from url, some other configuration also needs in config.php file inside application/config folder.

1. Open application/config/config.php from your codeigniter setup.
2. Search for $config['index_page'] = 'index.php'; and remove index.php from that syntax.
3. Search for $config['uri_protocol'] = ''; and add uri protocol AUTO.

thats it after this small changes you will access your controllers directly without using index.php.

ex. http://example.com/news/article/my_article

Sharing Info Cookie Consent

Our website uses cookies to improve your browsing experience and relevant content delivery. Before continuing to use our website, you agree & accept of our Cookie Policy & Privacy.