Skip to main content

[Ubuntu/Debian] Rewrite URLs on Apache Server

Updated over a week ago

Introduction

In this tutorial, you will learn how to activate and manage URL rewrites using the mod_rewrite module. Simply, mod_rewrite is used for rewriting a URL at the server level, providing the user with the output for that final page.

mod_rewrite provides a way to modify incoming URL requests dynamically, based on regular expression rules. This allows you to map arbitrary URLs onto your internal URL structure in any way you like.

It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule, providing a highly flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, including server variables, environment variables, HTTP headers, timestamps, external database lookups, and other external programs or handlers, which can be used to achieve granular URL matching.

The only requirement is to have a user with sudo privileges (root is fine) and Apache installed on your VPS.

1. Starting from updating and upgrading the system:

sudo apt update
sudo apt upgrade

2. Enabling mod_rewrite with:

sudo a2enmod rewrite

Output:

If you are asked to restart Apache, run:

systemctl restart apache2

mod_rewrite is now fully enabled.

3. Setting the .htaccess rules:

This file enables us to modify our rewrite rules without requiring access to server configuration files. For this reason, .htaccess is critical to your web application's security. By default, Apache prohibits the use of an .htaccess file to apply rewrite rules, so you first need to allow changes to the file.

Open the default Apache configuration file:

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

Inside that file, you will find a <VirtualHost *:80> block starting on the first line:

Add the following to the file:

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

As a result, it is:

Save and close the file.

Restart Apache to validate the changes:

sudo systemctl restart apache2

Now, create the .htaccess file in the web root (if you had installed WordPress, Joomla, and so on, it's already created):

sudo nano /var/www/html/.htaccess

Add the line as below:

RewriteEngine on

It should be like:

It's ready! We can now start using rewrite rules.

4. Configuring URL Rewrites

To illustrate that, let's use an example. We will make the file accessible via http://IP_of_your_server/example.html and also via http://IP_of_your_server/example.

Start by creating a file named example.html in the web root.

sudo nano /var/www/html/example.html

Add the following HTML code into the file, save it, and then close it:

<html>
<head>
<title>Example</title>
</head>
<body>
<h1>Example</h1>
</body>
</html>

It should be like:

You can access this page at http://IP_of_your_server/example.html; however, it can be inaccessible via IP address.

All RewriteRules abide by the following format:

  • RewriteRule specifies the directive.

  • Pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser.

  • Substitution is the path to the actual URL, i.e., the path of the file on Apache servers.

  • Flags are optional parameters that can modify how the rule works.

If the Example file is not accessible via IP address, we can resolve the issue using mod_rewrite rules. Open up the .htaccess file:

sudo nano /var/www/html/.htaccess

After the first line, add the following rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]

As a result, it will be:

Now, you should be able to access http://IP_of_your_server/example in your browser.

Conclusion

mod_rewrite is a functional Apache module that can be used effectively to ensure human-readable URLs.

If you'd like to learn more about mod_rewrite, you can take a look at Apache's mod_rewrite Introduction and Apache's official documentation for mod_rewrite.

Did this answer your question?