Rewrite URLs with mod_rewrite for Apache on Ubuntu 16 04

The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. It is possible to rewrite URLs with mod_rewrite module in Apache using .htaccess file.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Apache related queries.

In this context, we shall look into the method  to rewrite URLs with mod_rewrite in Apache.

How to rewrite URLs with mod_rewrite in Apache ?

Here, to rewrite URLs with mod_rewrite, we will use the following;

a. One Ubuntu 16.04 server set up with this initial server setup tutorial, including a sudo non-root user and firewall.

b. Apache 2 installed on your server. This requires Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04.

1. Enabling mod_rewrite in Apache

First, we need to activate the mod_rewrite so that Apache will understand the rewrite rules. Normally, it will be already installed. But will be disabled on a default Apache installation. So we run the below command to enable it.

$ sudo a2enmod rewrite

As a result, this will activate the module or alert that the module is already enabled. Then we restart the Apache so that the changes take effect.

$ sudo systemctl restart apache2

2. Setting Up .htaccess

We can modify the rewrite rules without accessing server configuration files by using the .htaccess file.

Before we start using the .htaccess file, we set up a few and secure a few more settings.

By default, Apache prohibits using a .htaccess file to apply rewrite rules. So first we allow changes to the file. For that, we open the default Apache configuration file using the nano text editor.

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

Inside that file, we will find a <VirtualHost *:80> block starting on the first line. Within that block, add the following new block so your configuration file looks like the following. Make sure that all blocks are properly indented.

<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
. . .

We save the file and close it. In order to put these changes into effect, we restart Apache.

$ sudo systemctl restart apache2

Now, we create a .htaccess file in the webroot.

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

Then we add this line at the top of the new file to activate the rewrite engine.

RewriteEngine on

Finally, we save the file and exit.

3. Configuring Rewrite URLs with mod_rewrite

Here, we will set up a basic URL rewrite that converts URLs into actual paths to pages. Specifically, we will allow users to access http://your_server_ip/about, but display a page called about.html.

First, we create a file named about.html in the webroot.

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

Next, we copy the following HTML code into the file, then we save and close it.

<title>About Us</title>
<h1>About Us</h1>

We can access this page at http://your_server_ip/about.html. But if we try to access http://your_server_ip/about, we will see a 404 Not Found error. To access the page using /about instead, we’ll create a rewrite rule.

Let's create our URL rewrite rule. For that, we open the .htaccess file.

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

After the first line (containing ‘RewriteEngine on’), we add the below line.

RewriteRule ^about$ about.html [NC]

In this case, ^about$ is the pattern, about.html is the substitution, and [NC] is a flag.

i. ^ indicates the start of the URL after your_server_ip/.

ii. $ indicates the end of the URL.

iii. about matches the string “about”.

iv. about.html is the actual file that the user accesses.

v. [NC] is a flag that makes the rule case insensitive.

We can now access http://your_server_ip/about in the browser. In fact, with the rule shown above, the following URLs will point to about.html:

http://your_server_ip/about, because of the rule definition.

http://your_server_ip/About, because the rule is case insensitive.

http://your_server_ip/about.html, because the original proper filename will always work.

However, the following will not work:

http://your_server_ip/about/, because the rule explicitly states that there may be nothing after about since the $ character appears after about.

http://your_server_ip/contact, because it won’t match the about string in the rule.

[Need urgent assistance with Apache queries? – We are here to help you.]


This article will guide you on how to rewrite URLs with mod_rewrite in #Apache. Basically, mod_rewrite helps in creating human-readable URLs.

The URL #Rewrite #Module is an extension software for #IIS (Internet Information Services). URLs should be created so that they are easy to remember for the users and easy to find for the search engines. The URL Rewrite Module enables web administrators to develop and implement rules that assist them in this task.

htaccess rewrite rule includes setting a combination of rewrite condition ( #RewriteCond ) tests along with a corresponding rule ( #RewriteRule ) if the prior conditions pass. In most cases, these rules should be placed at any point after the RewriteEngine line.

For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on

Also for Tech related tips, Visit or General Technical tips on