How to Rewrite URLs with mod_rewrite for Apache on Ubuntu 22.04

The mod_rewrite is a module in Apache that utilizes a rule-based rewriting engine. It is employed for converting and redirecting the requested URL to a different URL.

It enables the modification of a URL in real time. Consequently, the visitor will not observe any alterations to the URL in the address bar. By utilizing mod_rewrite, you can rewrite a limitless number of rules.

This will permit you to modify the URL according to environmental variables, HTTP headers, and server variables.

In this article, we will demonstrate the utilization of mod_rewrite to modify the URL for Apache on an Ubuntu machine.

Prerequisites

  • Ubuntu 22.04 with apache2 installed
  • SSH root access or a regular system user with sudo privileges

Step 1. Login to the server

First, log in to your Ubuntu 22.04 server through SSH as the root user:

ssh root@IP_Address -p Port_number

You must replace ‘IP_Address‘ and ‘Port_number‘ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the system user with sudo privileges.

You can check whether you have the proper Ubuntu version installed on your server with the following command:

# lsb_release -a

You should get this output:

No LSB modules are available.<br>Distributor ID: Ubuntu<br>Description: Ubuntu 22.04.2 LTS<br>Release: 22.04<br>Codename: jammy

Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:

# apt update -y

Step 2. Enable mod_rewrite

By default, the mod_rewrite module comes pre-installed with the Apache package but is deactivated. Therefore, you will need to activate it first.

# a2enmod rewrite

After running the command, you will be asked to restart Apache.

# systemctl restart apache2

Step 3. Enable .htaccess

You can configure rewrite rules directly in the primary Apache configuration file. However, creating rules in the .htaccess file within each website is advisable.

By default, Apache does not permit the use of the .htaccess file. Therefore, you must activate the .htaccess file in your default virtual host configuration file.

To accomplish this, modify the Apache default virtual host configuration file:

# nano /etc/apache2/sites-available/000-default.conf

Add the following:

Options Indexes FollowSymLinks MultiViews 
AllowOverride All
Require all granted

Save the file, then exit from the editor. And we need to restart the Apache service every time we make changes to apache configuration files.

# systemctl apache2 restart

At this point, the .htaccess file now overrides all other configurations, making URL rewrites possible.

mod_rewrite Directives

The .htaccess file is where the URL rewriting operations take place and contains a number of directives. Several of the available directives are:

  1. The RewriteEngine engine controls whether the runtime is enabled or disabled. the following sentence structure:
RewriteEngine On|Off

The RewriteEngine is not turned on by default. By turning the directive on or off, you can regulate runtime processing.

  1. The RewriteCond directive keeps track of the circumstances in which rewrites take place. Its syntax is as follows:
RewriteCond String ConditionPattern [Flags]

The directive keyword for a single condition is RewriteCond. Before a single rule, several condition directives are available.
The String includes server variables and backreferences in addition to plain text.
The String is subject to ConditionPattern, a Pearl Compatible Regular Expression (PCRE).

  1. The rewriting engine follows the RewriteRule rules. The syntax is as follows:
RewriteRule Pattern Substitution [Flags]

The directive keyword for a single rule instance is RewriteRule. Order plays a key role, and there are multiple alternative rules.
Pattern is a regex pattern that matches a specific section of a URL request. The mod_rewrite module uses the Pearl Compatible Regular Expression (PCRE) syntax.

The actual URL of the data we wish to display is substitution.
An optional parameter that modifies the behavior of the expression is [Flags].

Examples of URL Rewriting

Write Contents from One Page on Another

With the following URL rewriting, we can replace an existing page with new content without changing the URL.

RewriteEngine On<br>RewriteRule ^oldpage.html$ newcontent.html [NC]

With just two lines above, when visitors access oldpage.html, they will see the content of newcontent.html instead.

Rewrite the URL

Now, let’s create an html file called home.html.

# nano /var/www/html/home.html

Paste the following.

<html>
    <head>
        <title>Home</title>
    </head>
    <body>
        <h1>Home Page</h1>

<h2>This is the home page</h2>
    </body>
</html>

Save the file, then exit.

Now, let’s create a .htaccess file

# nano /var/www/html/.htaccess

Paste the following

RewriteEngine on<br>RewriteRule ^home$ home.html [NC]

Save the file, then exit.

At this point, when you access http://YOUR_IP_ADDRESS/home, you will see the content of http://YOUR_IP_ADDRESS/home.html

Hopefully, our guide on how to rewrite URLs with mod_rewrite for Apache on Ubuntu 22.04 was of help to you. We would love to hear from you now:

Did we skip something essential, or do you need a more detailed explanation about any of the steps? What are some other topics or tutorials you would want us to delve into?

Please, feel free to share your thoughts in the comment section.

Leave a Comment