Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  VPS FAQ  >  Learn to Set Up mod-rewrite in Simple Steps
Top Scroll

Learn to Set Up mod-rewrite in Simple Steps

 4 min

Apache’s mod_rewrite offers an effective and easy way for manipulating URLs. URLs are manipulated from the server side. In simple terms, mod_rewrite helps clean and user-friendly URLs to convert into websites. As per the name, it performs rewriting on URL. It can be a great way for cleaning up your website’s URLs. In this tutorial, you will learn about mod_rewrite, its usage and steps to set it up on a VPS running Apache!

The Working of Apache mod_rewrite

When a URL is entered, it is checked against a list of pre-defined rules. The rules are designed to check for particular patterns or keywords. If a particular keyword is present in the URL and the rule matches, it gets replaced with a pre-defined string – a new URL.

What Makes mod_rewrite Useful?

mod_rewrite allows users to manipulate URLs into clean URLs which is the biggest advantage of it. This is easily understandable by the end user who doesn’t have technical knowledge.

You will find that these URLs are very user as well as search friendly. These URLs can be quickly identified by the search engines. What is a clean URL? Let’s check the below example:

1. URL1:
2. URL2:
3. URL3:

From the three URLs listed above, you can see that the third one is much more readable and understandable to the end user as compared to the first and second. So, URL 3 is a clean URL here.

Steps to Set Up mod_rewrite on a Linux VPS

Prior to starting, you will need to access your VPS using SSH. Now let’s start the process:

1. Install Apache

In this example, we are using Ubuntu 18.04. It comprises of a built-in package installer – apt-get. First, you will need to update it by running the below command:

sudo apt-get update

Now you can proceed with apache2 installation:

Use the below command for installation:

sudo apt-get install apache2

2. Enable mod_rewrite

Now, you have to enable mod_rewrite with the below command:

sudo a2enmod rewrite

This will enable the rewrite mode or will let you know if it is already in use. Then restart Apache using this command:

sudo service apache2 restart

3. Create the .htaccess File

It is important for the URL rewrite rules to be pre-defined. This is where .htaccess comes into the picture. All the rules in the .htaccess file can be written by the user. As this file is used by the server, there should not be any errors on this file or else it will return a server error. Rewrite riles can be modified at any point in time.

It is important to create the .htacess file at the root to test the rewrite functionality.

First run the below command:

sudo nano /var/www/html/.htaccess

With this command, the .htaccess file will be created if it is not yet present, or opened it if it exists. Now, just save and exit it. In nano, you just have to press CTRL+O to save, and CTRL+X to exit.

Next open the 000-default.conf file located in the /etc/apache2/sites-enabled/directory. This can be done with the below command:

sudo nano /etc/apache2/sites-enabled/000-default.conf

Inside this file, type the below block after the string :

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

Save the file similar to the .htaccess. For the changes to take effect, restart Apache as mentioned in step two above.

4. URL Rewrite

URL rewrite basically selects the clean URL and converts it to actual paths leading to code. It must have the below components:

• A pre-defined rewrite rule.
• A pattern – the pattern provided will act as a matching reference with the URL, the user enters.
• Rewriting lines – it will call the path that should be called by the server at that point of time.

Now, you will write a rewrite rule which will redirect a user to About_us.html page, if the requested URL is http://ip/Aboutus

For successful running of rewrite rule, the rewrite engine should be on. Run the below command at the start of the .htaccess file.

RewriteEngine on

Next, add the below syntax:

ReWriteRule ^About_us$ Aboutus.html [NC]

Let’s understand the syntax in a simple way:

• About_us is the pattern that will be redirected to About_us.html when encountered and matched.
• NC is a flag for making the rule case insensitive.
• ^ Indicates that the text immediately after the IP address is matched.

When you combine the two lines it would appear as below in the .htaccess file:

ReWriteEngine on
ReWriteRule ^About_us$ Abouts_us.html [NC]

That’s it! You have successfully created a mod_rewrite rule!

Wrap Up

From this, you have seen that easy, clean and user-friendly URLs are key for any successful website. Having keywords is very important for memorable URLs as well as SEO too! Hundreds of rules can be created and written.

We hope you will use mod_rewrite for good purpose.

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
Claim the discount before it’s too late. Use the coupon code:
Note: Copy the coupon code and apply it on checkout.