Apache mod_rewrite Module is used to rewrite requested URLs on the fly. This mod_rewrite module provides an easy way to manipulate URLs using an unlimited number of rules. A rewrite rule is invoked in per-server context (httpd.conf
) and per-directory context (.htaccess
). In order to work with URL rewrite rule, you need to enable mod_rewrite in Apache server. Rewrite URLs mainly used for providing clean, human readable, and SEO friendly URL.
In this tutorial, we’ll learn how to setup and enable mod_rewrite in Apache on Ubuntu 14.04. Follow the step-by-step guide to enable the mod_rewrite module in Apache, it will allow you to rewrite URLs on your web application.
Open the Terminal and type the following command for the respective task.
If you have not already Apache installed, install the Apache on Ubuntu first.
sudo apt-get update
sudo apt-get install apache2
Activate the mod_rewrite module in Apache.
sudo a2enmod rewrite
Once mod_rewrite is enabled successfully, restart the Apache.
sudo service apache2 restart
A .htaccess
file allows us to define rewrite rules without modifying the server configuration file. Before creating a .htaccess
file, we’ll need to do some settings.
At first update permissions to allow changes in the default Apache configuration file.
sudo chmod 777 /etc/apache2/sites-available/000-default.conf
Open the default Apache configuration file.
sudo nano /etc/apache2/sites-enabled/000-default.conf
Inside the <VirtualHost *:80> block, add the following block.
<Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
Save and exit the file.
Run the following command to update permissions to ensure that the user can only read this file.
sudo chmod 644 /etc/apache2/sites-available/000-default.conf
Restart the Apache server.
sudo service apache2 restart
Create the .htaccess file.
sudo nano /var/www/html/.htaccess
To activate RewriteEngine, add the following line at the top in the .htaccess
file.
RewriteEngine on
Save and exit the file.
Run the following command to update permissions to ensure that the user can only read this file.
sudo chmod 644 /var/www/html/.htaccess
Now you’ll be able to define rewrite rules and working with URL rewrite.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Thanks Really helpful