Hidden index.php in CodeIgniter using .htaccess

Now I will explain how to hidding index.php if we access url in Codeigniter. Example, I want to access http://localhost/codeigniter/index.php/login with http://localhost/codeigniter/login without index.php. So if you are using Linux such as Ubuntu. You should take the following instructions:

  1. Open apache2 configuration:

    aim@aim-tian:~$ sudo gedit /etc/apache2/sites-available/default

    And then change this configuration such as:

    <VirtualHost *:80>
    	ServerAdmin webmaster@localhost
    
    	DocumentRoot /var/www
    	<Directory />
    		Options FollowSymLinks
    		AllowOverride All
    	</Directory>
    	<Directory /var/www/>
    		Options Indexes FollowSymLinks MultiViews
    		AllowOverride All
    		Order allow,deny
    		allow from all
    	</Directory>
    
    	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    	<Directory "/usr/lib/cgi-bin">
    		AllowOverride None
    		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    		Order allow,deny
    		Allow from all
    	</Directory>
    
    	ErrorLog ${APACHE_LOG_DIR}/error.log
    
    	# Possible values include: debug, info, notice, warn, error, crit,
    	# alert, emerg.
    	LogLevel warn
    
    	CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    And then open other configuration:

    aim@aim-tian:~$ sudo gedit /etc/apache2/sites-enabled/000-default

    Change configuration such as:

    <VirtualHost *:80>
    	ServerAdmin webmaster@localhost
    
    	DocumentRoot /var/www
    	<Directory />
    		Options FollowSymLinks
    		AllowOverride All
    	</Directory>
    	<Directory /var/www/>
    		Options Indexes FollowSymLinks MultiViews
    		AllowOverride All
    		Order allow,deny
    		allow from all
    	</Directory>
    
    	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    	<Directory "/usr/lib/cgi-bin">
    		AllowOverride None
    		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    		Order allow,deny
    		Allow from all
    	</Directory>
    
    	ErrorLog ${APACHE_LOG_DIR}/error.log
    
    	# Possible values include: debug, info, notice, warn, error, crit,
    	# alert, emerg.
    	LogLevel warn
    
    	CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    Don’t forget to save all configuration. 🙂

  2. And then, we must activated library mod_rewrite.so. We must have to check the location of the previous file:

    aim@aim-tian:~$ locate mod_rewrite.so
    /usr/lib/apache2/modules/mod_rewrite.so
    aim@aim-tian:~$

    From this result, we know that mod_rewrite.so has existed in the directory /user/lib/apache2/modules. And now we must activated it:

    aim@aim-tian:~$ sudo gedit /etc/apache2/mods-enabled/rewrite.load

    Now paste this following line

    LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
    

    And we must change too on:

    aim@aim-tian:~$ sudo gedit /etc/apache2/mods-available/rewrite.load

    Now paste this following line

    LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
    

    Don’t forget save and restart your apache:

    aim@aim-tian:~$ sudo service apache2 restart
    * Restarting web server apache2 apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
    … waiting apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
    [ OK ]
    aim@aim-tian:~$

  3. And the last, we must create .htaccess file on your apps. .htaccess:
    Options FollowSymLinks
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule ^(applications|system|\.svn) index.php/$1 [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    </IfModule>
    <IfModule !mod_rewrite.c>
        ErrorDocument 404 /index.php
    </IfModule>
    

    See this picture:
    htacces

And finish…., And then open your apps, example http://localhost/codeigniter/login without index.php. I hope this article can help you.. 😀

Advertisements

9 comments

  1. I’m not sure where you’re getting your information, but great topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for fantastic info I was looking for this information for my mission.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s