How To Set Up mod_rewrite for Apache on Ubuntu

ayekpleclemence

Ayekple Clemence

Posted on January 4, 2020

How To Set Up mod_rewrite for Apache on Ubuntu

Introduction

In this tutorial, we learn how to use mod_rewrite module to manage URL rewrites using Apache2. The module allows us to rewrite URLs in a fashionable, that is converting URLs to human-readable paths.

Just think of the last time you visited a blog site to read an article, looking for that particular article to read. When you finally reached the article, the URL looked like this:

domain.com/latest-article/title-of-the-article-your-are-looking-for

That is the work of mod_rewrite. This tutorial will discuss how to activate mod_rewrite, create and use the required .htaccess page and finally set up the URL rewrites.

Prerequisites

To follow this tutorial, you will need:

  • Ubuntu
  • sudo privilege

Step 1 – Installing Apache

In this step, the system required the user to have root privileges.

First, update the system’s package index with the following command:

sudo apt-get update

This will ensure that all packages are up to date.

Then we can now install Apache2 which is the HTTP server and the world most commonly used. Run the following command to install it:

sudo apt-get install apache2

Step 2 – Activating mod_rewrites

Before rewriting our URLs, we need to activate the apache mod_rewrite module that controls the URL rewrite.

sudo a2enmod rewrite

The command activates the module or—if it is already activated, displays the message, “Module rewrite already enabled”. To put these changes into effect, we need to restart Apache.

sudo service apache2 restart

Step 3 — Setting Up .htaccess

In this step, we will setup a .htaccess file for managing the rewrite rule. A .htaccess file makes it possible for us to configure the details of a website without actually altering the server configuration files. For this reason, .htaccess is critical to your web application’s security. The .htaccess file will be hidden within the file because of the period that starts it.

Additionally, the location of the .htaccess file is very important. This is because the configurations in the .htaccess will affect everything in its directory and the subdirectories.

You can use any text editor of your choice to create the .htaccess file (NB: the name is only .htaccess without any other extension or name).

Alternatively, the following command will help you create the .htaccess file. You would have to replace the example.com with the name of your site’s folder.

sudo nano /var/www/html/domain.com/.htaccess

Permitting changes in the .htaccess file

We will need to do a few more changes in our set up and secure a few more settings before we can begin.

First and foremost, let’s allow changes in the .htaccess file. Use your favorite text editor to open the default Apache configuration file or you follow along as we use nano to edit our files.

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

Edit your code to look like the following block of code below:

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

Now save the file and exit, restart Apache with the following command:

sudo service apache2 restart

The .htaccess file should now be available for all your sites

Now we are set to rewrite our site’s URLs.

Step 4 – Rewriting URLS

The .htaccess file controls the entire operation of the URL rewriting.
The URL rewrite commands follow the same pattern:
RewriteRule Pattern Substitution [OptionalFlags]

A short explanation on the rewrite part:

  • RewriteRule: specifies the directive RewriteRule
  • pattern: a regular expression that matches the desired string
  • substitution: path to the actual URL
  • OptionalFlags: optional parameters that can modify the rule

When mod_rewrite is not configured for a web application, the application default to using query strings, which are usually appended to the URL using the question mark ? and the delimiter ampersand &. These query strings are ignored when matching rewrite rules. However, you might need query strings in order pass data between web pages. For instance, a search result page written in PHP may look like the following:

http://domain.com/search.php?name=kofi&age=30

In this example, we would like to simplify this to become:

http://domain.com/kofi/30

Simple Replacement Using rewrite

Using a rewrite rule, we could use the following:

/var/www/html/domain.com/.htaccess

RewriteRule ^kofi/30$ search.php?name=kofi&age=30

As it actually maps kofi/30 to search.php?name=kofi&age=30. And that is what we want to achieve.

In this example, ^kofi/30$ is the pattern, search.php?name=kofi&age=30 is the substitution.

The following special characters were used in the example above:

  • ^ indicates the start of the URL, after http://domain.com/.
  • $ indicates the end of the URL.
  • kofi/30 matches the string “kofi/30”.
  • search.php?name=kofi&age=30 is the actual file that the user accesses.

With this example, you should now be able to access http://domain/kofi/30 in your browser.

We now have a simple rule in our .htaccess file that can be modified and extended to meet your needs.

💖 💪 🙅 🚩
ayekpleclemence
Ayekple Clemence

Posted on January 4, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related