Apache’s mod_rewrite offers a good and simple approach for manipulating URLs. URLs area unit manipulated from the server-side. In easy terms, mod_rewrite helps clean and easy URLs to convert into websites. As per the name, it performs revising on a uniform resource locator. It will be an excellent approach for improvement up your website’s URLs. during this tutorial, you'll study mod_rewrite, its usage, and steps to line it au courant a VPS running Apache!
The operating of Apache mod_rewrite
When a uniform resource locator is entered, it's checked against an inventory of pre-defined rules. the principles area unit designed to examine specific patterns or keywords. If a specific keyword is gift within the uniform resource locator and also the rule matches, it gets replaced with a pre-defined string – a replacement uniform resource locator.
What Makes mod_rewrite Useful?
mod_rewrite permits users to govern URLs into clean URLs that is that the biggest advantage of it. this is often simply intelligible by the end-user United Nations agency doesn’t have technical data.
You will realize that these URLs area unit terribly helpful similarly as search-friendly. These URLs will be quickly known by search engines. what's a clean URL? Let’s check the below example:
1. URL1: http://modrewriteexample.com/client.php?id=A786#234QA
2. URL2: http://modrewriteexample.com/client/=A786#234QA/
3. URL3: http://modrewriteexample.com/client/Martha/
From the 3 URLs listed higher than, you'll be able to see that the third one is way additional decipherable and intelligible to the end-user as compared to the primary and second. So, uniform resource locator three could be a clean uniform resource locator here.
Steps to line Up mod_rewrite on a UNIX operating system VPS
Prior to beginning, you'll get to access your VPS victimization SSH. currently, let’s begin the process:
Install Apache
In this example, we tend to area unit victimization Ubuntu eighteen.04. It includes an integral package installer – apt-get. First, you'll get to update it by running the below command:
1 | sudo apt-get update |
Now you'll proceed with apache2 installation:
Use the below command for installation:
1 | sudo apt-get install apache2 |
2. alter mod_rewrite
Now, you have got to alter mod_rewrite with the below command:
1 | sudo a2enmod rewrite |
This can alter the rewrite mode or will allow you to apprehend if it's already in use. Then restart Apache exploitation this command:
1 | sudo service apache2 restart |
3. Create the .htaccess File
It is vital for the computer address rewrite rules to be pre-defined. this can be wherever .htaccess comes into the image. All the foundations within the .htaccess file are written by the user. As this file is employed by the server, there shouldn't be any errors on this file as an alternative it'll come as a server error. Rewrite riles is changed at any purpose in time.
It is vital to form the .htacess file on the basis to check the rewrite practicality.
First, run the below command:
1 | sudo nano /var/www/html/.htaccess |
With this command, the .htaccess file is created if it does not nevertheless gift or opened if it exists. Now, simply save and exit it. In nano, you only got to press CTRL+O to save lots of, and CTRL+X to exit.
Next, open the 000-default.conf file placed within the /etc/apache2/sites-enabled/directory. this will be through with the below command:
1 | sudo nano /etc/apache2/sites-enabled/000-default.conf |
Inside this file, type the below block after the string :
1 | <Directory /var/www/html> |
2 | Options Indexes FollowSymLinks MultiViews |
3 | AllowOverride All |
4 | Order allow, deny |
5 | allow from all |
6 | </Directory> |
Save the file just like the .htaccess. For the changes to require impact, restart Apache as mentioned in step 2 on top of.
4. computer address Rewrite
URL rewrite essentially selects the clean computer address and converts it to actual ways resulting in code. It should have the below components:
• A pre-defined rewrite rule.
• A pattern – the pattern provided can act as an identical reference with the computer address, the user enters.
• revising lines – it'll decide the trail that ought to be known as by the server at that time of your time.
Now, you'll write a rewrite rule which can send a user to the About_us.html page, if the requested computer address is http://ip/Aboutus
For the self-made running of the rewrite rule, the rewrite engine ought to air. Run the below command at the beginning of the .htaccess file.
1 | RewriteEngine on |
Next, add the below syntax:
1 | ReWriteRule ^About_us$ Aboutus.html [NC] |
Let’s understand the syntax in a very straightforward way:
• About_us is that the pattern that may be redirected to About_us.html once encountered and matched.
• NC could be a flag for creating the rule case insensitive.
• ^ Indicates that the text instantly when the information science address is matched.
When you mix the 2 lines it might seem as below within the .htaccess file:
1 | ReWriteEngine on |
2 | ReWriteRule ^About_us$ Abouts_us.html [NC] |
That’s it! You have successfully created a mod_rewrite rule!