How to Enable Apache Mod_Rewrite | HostAdvice

How to Enable Apache Mod_Rewrite on an Ubuntu 18.04 VPS or Dedicated Server

Mod_rewrite is a powerful Apache module that provides URL manipulation capability. The sophisticated feature allows webmasters to rewrite URLs and this is a common practice in many content management systems coding like WordPress.

Mod_rewrite is famous for translating human-readable URLs in dynamic websites. This makes the URL’s look cleaner and friendly in websites.

In this guide, we are going to cover the steps of enabling mod_rewrite on your Apache Server running on an Ubuntu 18.04 VPS.

Prerequisites

  • Ubuntu 18.04 VPS
  • A non-root user with sudo privileges
  • Apache web server

Step 1: Enable mod_rewrite

You can enable any Apache module using the a2enmod command. So run the command below on your Ubuntu 18.04 server:

$ sudo a2enmod rewrite

In case the module is already enabled on your server, you will get an alert message.

You must restart Apache once you make any change to its configuration. To do this, type the command below on a terminal window:

$ sudo systemctl restart apache2

Your server is now ready to accept rewrite rules.

Step 2: Setup your server to accept .htaccess files

You can set up URL rewrite rules directly on Apache’s configuration file. However, it is advisable to keep the rules in ‘.htaccess’ file on each website. Most content management systems rely on the ‘.htaccess’ file and it is created by default once you install the applications.

By default, Apache does not allow the use of ‘.htaccess’ file so you will need to edit the configuration of each website’s virtual host file by adding the following code:

<Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
</Directory>

For instance, you can edit the default virtual hosts that ships with Apache using a nano editor by typing the command below:

$ sudo nano /etc/apache2/sites-available/000-default.conf

Then copy paste the above text just before the ‘</VirtualHost>’ closing tag. Remember to save the file by pressing CTRL + X then Y and Enter

Then, restart Apache for the changes to take effect:

$ sudo systemctl restart apache2

Step 3: Mod-rewrite syntax

The basic Apache mod_rewrite syntax has the following parts:

RewriteRule pattern substitution [flags]

  • RewriteRule – the directive of our rule.
  • Pattern – this is a regex (Regular Expression) that matches what the user types in a browser.
  • Substitution – The actual URL path that should be called by the Apache server.
  • Flag – optional parameters that modify how the rules work..

Step 4: Create a sample .htaccess file

We will now create a sample ‘.htaccess’ file at the root of the default website to test mod_rewrite. To do this type the command below

$ sudo nano /var/www/html/.htaccess

Every mod_rewrite rules must be with the commands ‘RewriteEngine on’. So you need to type this at the top of the file.

RewriteEngine on

Next, we are going to rewrite a rule that redirects users to a ‘contact_us.html’ page if they request the URL http://ipaddress/contact_us

So we add the below rule:

RewriteRule ^contact_us$ contact_us.html [NC]

In the above rule, ‘contact_us’ is the pattern that should be matched and redirected to our substitution path ‘contact_us.html’. The command ‘[NC]’is a flag that tells Apache to make the rule case insensitive. ‘^’ indicates that we are matching any text after the server public IP address or domain name while ‘$’ signifies the end of the URL that we are matching.

So our complete ‘.htaccess’ a file should look like the text below:

RewriteEngine on
RewriteRule ^contact_us$ contact_us.html [NC]

Save the file by pressing CTRL+ X, Y, and Enter.

Next type the command below to create the contact_us.html page:

$ sudo nano /var/www/html/contact_us.html

Then, paste the HTML text below on the file:

<html>
    <head>
        <title>Contact our website</title>
    </head>
    <body>
        <h1>This is a contact us page</h1>
    </body>
</html>

Save the file by pressing CTRL+ X, Y and Enter.
Now if you visit the path http://ipaddress/contact_us on a browser, Apache should serve you with the page ‘contact_us.html’ that we created like shown below:

Conclusion

Meaningful URLs are very important both to humans and search engines. In fact, including keywords on the URLs instead of numbers make your website SEO-friendly. There dozens of rules that you can write on your Apache web server but the above guide was a simple tutorial for enabling mod_rewrite on your machine and making sure the module works as expected.

If you were unable to run a content management system like WordPress before, it should now work. We hope you enjoyed reading the guide and you are going to apply the steps learned on your Ubuntu 18.04 VPS.

Check out these top 3 Linux hosting services

FastComet
$1.79 /mo
Starting price
Visit FastComet
Rating based on expert review
  • User Friendly
    4.7
  • Support
    5.0
  • Features
    4.8
  • Reliability
    4.5
  • Pricing
    5.0
Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0

How to Host Multiple Websites on an Ubuntu 18.04 VPS or Dedicated Server

This article will show you how to host two or more websites on a single Virtual
5 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Enable Two-Factor Authentication on an Ubuntu 18.04 VPS or Dedicated Server

This guide will show you how you enable two-factor authentication to improve the
4 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Install Apache Cassandra on an Ubuntu 18.04 VPS or Dedicated Server

This tutorial will help you install and configure Apache Cassandra (an opensourc
3 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Install a Let’s Encrypt Certificate on your Ubuntu 18.04 Dedicated Server or VPS

If you are hosting your website on a VPS server running Ubuntu 18.04, we will sh
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top