Ad Code

Ticker

6/recent/ticker-posts

Codeigniter how to remove index.php from url

 

Codeigniter how to remove index.php from url

Codeigniter is a popular PHP framework and it has build-id form validation, routing, database connectivity it has also an MVC model to support developing large-scale projects.

When CodeIgniter or any framework run then the first file running is index.php in every framework or PHP project, but if you want to remove it and modify the URL then you can use some config files:-

  1. For Apache:- use .htaccess file for URL rewrite
  2. For Nginx:- use Nginx config file in the system to write a config for the particular project
  3. For IIS (Window Server):- use the web.config file for URL rewrite

In Apache remove the index.php

In the apache server firstly you have to config basic settings to allow .htaccess rewrite and config in your projects:-

  1. Open CodeIgniter project directory and go to application/config directory to open config.php and change code
    $config['index_page'] = "index.php"
    # to
    $config['index_page'] = ""

  2. In the same file you can change this code also
    $config['uri_protocol'] ="AUTO"
    # to
    $config['uri_protocol'] = "REQUEST_URI"

  3. After that, you can go to the root folder of CodeIgniter and open/create .htaccess file and put any of these codes that you check to run perfectly in your project version and sub-directory
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L] 
    </IfModule>
    
    # OR
    
    <IfModule mod_rewrite.c>
      RewriteEngine On
      #RewriteBase /
    
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^ index.php [QSA,L]
    </IfModule>
    
    # OR
    
    # for sub directory project htaccess
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /sub_dir/
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

In Nginx remove the index.php

This also follows the previous 2 points and after you can open the server config of the Nginx file which allow you to default and write this code
server {
        server_name domain.tld; #you project domain like example.com

        root /var/www/codeignitor; #your project root directory
        index index.html index.php;

        # set expiration of assets to MAX for caching
        location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
                expires max;
                log_not_found off;
        }

        location / {
                # Check if a file or directory index file exists, else route it to index.php.
                try_files $uri $uri/ /index.php;
        }
		
        location ~* \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                include fastcgi.conf;
        }
}


In IIS(Window Server) remove the index.php

This also follows the previous 2 points and after you can open the project root directory and create a web.config file and write this code
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <system.webServer>

        <httpErrors errorMode="Detailed" />
        <asp scriptErrorSentToBrowser="true"/>

        <rewrite>
        <rules>
            <rule name="RuleRemoveIndex" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                </conditions>
                <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true"/>
            </rule>
        </rules>
        </rewrite>

    </system.webServer>

    <system.web>
        <customErrors mode="Off"/>
        <compilation debug="true"/>
    </system.web>

</configuration>
--
All of the above we have to learn how to remove index.php from URL in CodeIgniter with 3 different servers, if you have any error regarding, this you can comment below, thanks.

Post a Comment

0 Comments

Ad Code