Remove Index.php From URLs
Published on Mon - 3rd Oct 2016
3753 views
How does Beutiful URLs sound?
Think about the last time you visited some famous websites , looking for that one specific thing you needed to rent or buy. When you finally reached the page, the URL most likely looked something like this:
http://example.com/cars/bmw
Well, Believe it or not , This does not happen because the webmaster took the time to set up every single directory you would need to make your renting or buying , but its because of a handy module called mod_rewrite
.
Okay .. What does Mod_Rewrite do?
mod_rewrite
allows you to make custom and simplified URLs as needed. In reality, the actual URL may have looked closer to this:
http://example.com/18273/cars/?ref=2828389&car_id=92882
Well in This tutorial , We will check on how to activate mod_rewrite
, Creating and Using the required .htaccess page, and setting up the URL rewrites :)
Now How to do it ?
Here is what you are going to do , Check if you have Mod Rewrite On
apachectl -M
If you are running a Centos Machine, You can run this via command line
httpd -l
You could do it inside PHP like this
in_array('mod_rewrite', apache_get_modules());
If for some reason you have not activated , You could try Activating it By Doing this on your Shell
sudo a2enmod rewrite
The above command does the required job well —if it is already activated, displays the words, "Module rewrite already enabled" , That way you will know when its not activated , Very usefull indeed.
Now We have that sorted.
Lets talk about the .htaccess File
Once the module has been activated, you can set up your URL rewrites by creating this file in your root directory that way you will be able to remove index.php from your URLs.
An .htaccess is a configuration file for use on web servers running the Apache Web Server software
These types of files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer.
There are alot of tutorials out there about this file.
We need to permit our virtual host to be able to override this behaviour
Now , To allow the .htaccess
file to override standard website configs, start by opening up the configuration file.
sudo nano /etc/apache2/sites-available/default
Once inside that file, find the following section, and change the line that says AllowOverride from None to All. The section should now look like this:
Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all
After you save and exit that file, restart apache..htaccess
files will now be available for all of your sites.
sudo service apache2 restart?
Or
#Restarts Apache systemctl restart httpd #Or You can also do this apachectl stop apachectl start #Or a Graceful one sudo service httpd graceful
Now you are all set up to rewrite your site’s URLs, Check out the next step below Removing “index.php” from URLs
Beutify Your URLs (Remove That Index.php On our Pages)
Now lets talk about how we can remove the index.php from our URLs (I know we 've being saying that alot but bear with us on this one :D)
Lets head to this file
sudo nano /var/www/example.com/.htaccess #or sudo nano /home/example.com/public_html/.htaccess
This is the basic rule to hide index.php
from the URL. Put this in your root .htaccess
file.
mod_rewrite
must be enabled with PHP and this will work for the PHP version >= 5.2.x
RewriteEngine On
# Un Comment if you have on a subdomain
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Or you coud replace the last line with one
RewriteRule ^(.*)$ index.php?/$1 [L]
The Above techniques has been used by Major PHP Frameworks Including Codeigniter , Express Engine and Even Laravel , And hey ... You do not need Framework to Apply this technique , works also on Vanilla PHP
Happy Beatifying your URLs!
And that How you can remove index.php from URLs using the famous .htaccess File