What is mod_rewrite ?

It is a software component that performs rewriting on Uniform Resource Locators, modifying their appearance. This modification is called URL rewriting. It is a way of implementing URL mapping or routing within a web application.

Lets begin with setting up mod_rewrite.
please note that you have to have root privileges in order to proceed & need to get apache installed first if you haven’t done so yet.

sudo apt-get install apache2

Now you need to install mod_rewrite first.

sudo a2enmod rewrite

Now this command should activate the module if its already active then you should see this message.

Module rewrite already enabled

Now you need to create .htaccess file in the root directory where files are located to by accessible by specific domain or IP.

cd /to/root/directory/

in the most cases its /var/www/html, /var/www/yourdomain.com or inside /home/ you can also ask your hosting provider regarding your root directory.

Now create .htaccess using following command and add your rewrite rules there

sudo nano .htaccess

Now you need to allow .htaccess file that you created to override default configs cd to / (root) first then do this

sudo nano /etc/apache2/sites-available/default

Look for section written below and change the line that says AllowOverride from None to All. The section should now look like this:

this is location of the root path of your site /var/www/

 <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all

And finally run this command in order to restart the server

sudo service apache2 restart

Congratulations! you are all set with setting up mod_rewrite.

(Visited 22 times, 1 visits today)