Knowledge Base
MilesWeb / VPS FAQ

Learn to Set Up mod-rewrite in Simple Steps

Approx. read time : 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: 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 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
</Directory>

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.

Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.

Trusted By Thousands of Clients & Big Businesses

We highly appreciate the kind and stellar feedback we receive from our customers. Delivering the best is our goal! MilesWeb is rated Excellent out of 5 based on reviews. Read more reviews.

Excellent
Based on reviews
2 hours ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
4 hours ago
Milesweb is superb Hosting pro...
Milesweb is superb Hosting provider ever, their Support team is amazing!!!...
Abhishek Singh
15 hours ago
Great support in great timing...
We need urgent assistance on changes in a primary domain on our client's Cpanel accounts and reached...
Riyaju Deen
21 hours ago
Best Website Hosting platform ...
I was new on MilesWeb. And needed help on multiple areas from setting up to getting started with cre...
Harshada
1 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
1 days ago
the team is very supportive th...
the team is very supportive though at times effort needs to be made to make understand the problem s...
Suree Sharma
1 days ago
I am using miles web for 3plus...
I am using miles web for 3plus years, very quick and perfect support by the team, they helped me man...
Sri Raghav
2 days ago
The service is good...
The service is good. They are answering with patience and doing the needful as soon as possible....
MR
2 days ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
3 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
4 days ago
positively helped me with find...
positively helped me with finding insecure content on my website causing SSL to not work properly on...
Thaviraj Junglee
4 days ago
Exceptional support, Truly Pra...
I had opted for the basic wordpress hosting plan as I intended to experiment with various plug-ins. ...
Aseem Chandna