Apache‘s mod_rewrite is an effective and easy way to manipulate URLs. URL manipulation is done from the server side. In a simple illustration, with mod_rewrite, clean and user-friendly URLs are converted into websites. As the name suggests, it performs rewriting on URL. It can be a great way to clean up your website’s URLs. In this tutorial, we’ll explain what mod_rewrite is, why it is useful, and how to set it up on a VPS running Apache!
How Does Apache mod_rewrite Works?
Whenever a user enters a URL, it is checked against a list of pre-defined rules. The rules are present to look for particular patterns or keywords. If that keyword is present in the URL and the rule matches, it is replaced with a pre-defined string – a new URL.
What Makes mod_rewrite Useful?
The biggest advantage mod_rewrite offers is its ability to manipulate URLs into clean URLs, which can be easily understood by the end user who might not be a techie.
These URL’s also, in turn, are very user and search friendly. Search engines recognize these URLs quicker! What is a clean URL? Let’s have a look at this example:
- URL1: http://modrewriteexample.com/client.php?id=A786#234QA
- URL2: http://modrewriteexample.com/client/=A786#234QA/
- URL3: http://modrewriteexample.com/client/Martha/
From the above three URLs listed above, it is clear that the third one is much more readable and understandable to the end user than the first and second. URL 3 is a clean URL.
How to Set Up mod_rewrite on a Linux VPS?
Remember, before we start, you need to access your VPS using SSH! Check out our PuTTY tutorial if you’re having trouble. Now let’s start the process:
1. Install Apache
We’ll use Ubuntu 18.04 for this example. It has a built-in package installer – apt-get. First, we will update it by running the command:
sudo apt-get update
Now we can proceed with installing apache2:
The command for installation is:
sudo apt-get install apache2
2. Enable mod_rewrite
Now, we will enable mod_rewrite.
The command for enabling it is:
sudo a2enmod rewrite
The above command will enable the rewrite mode or will let you know if it is already in use. After this restart Apache:
sudo service apache2 restart
3. Create the .htaccess file
URL rewrite rules must be pre-defined. Here, .htaccess comes into the picture. A user can write all the rules in the .htaccess file. It is used by the server. There should not be any errors on this file, else it will return a server error. We can modify our rewrite rules at any point in time.
The .htacess file must be created at the root to test the rewrite functionality.
First run the below command:
sudo nano /var/www/html/.htaccess
This will create the .htaccess file if it is not yet present, or open it if it exists. For now we can save and exit. In nano you can do it by hitting CTRL+O to save, and CTRL+X to exit.
Next we need to open the 000-default.conf file located in the /etc/apache2/sites-enabled/ directory. We can do it with the command:
sudo nano /etc/apache2/sites-enabled/000-default.conf
Inside this file place the below block after the string <VirtualHost *:80>:
<Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow, deny allow from all </Directory>
Save the file as you did with .htaccess. In order for the above changes to take effect, restart Apache as mentioned in step two above.
4. URL Rewrite
URL rewrite basically picks the clean URL and converts them to actual paths leading to code. It must have:
- A pre-defined rewrite rule
- A pattern – the pattern provided will act as a matching reference with the URL entered by the user
- Rewriting lines – it will call the path that should be called at that point of time by the server
Now, we will write a rewrite rule which will redirect a user to About_us.html page, if the requested URL is http://ip/Aboutus
To successfully run the rewrite rule, the rewrite engine should be on. Enter the below command at the start of the .htaccess file.
The next like can be our rule:
ReWriteRule ^About_us$ Aboutus.html [NC]
The syntax might be a bit confusing, let’s break it down:
- In the above rule, About_us is the pattern, which when encountered and matched will be redirected to About_us.html
- NC is a flag which makes the rule case insensitive.
- ^ Depicts that the text immediately after the IP address is matched.
The two lines combined would look like this in the .htaccess file:
ReWriteEngine on ReWriteRule ^About_us$ Abouts_us.html [NC]
There we go, that’s it! We successfully created a mod_rewrite rule!
Easy, clean and user-friendly URLs play a vital role for any successful website. Having keywords is very important, not just for memorable URLs, but SEO too! There are hundreds of rules you can create and write. And now you know how! We hope you put mod_rewrite to good use! See you in the next tutorial.