How to Rewrite URLs using mod_rewrite for Apache on Ubuntu 18.04


rewrite urls using mod_rewrite for apache on ubuntu

There are many instances when you need rewrite/redirect URLs, as your website grows, or if you modify its site structure. Apache provides mod_rewrite module that makes it really easy to rewrite URLs. In fact, it also allows you to setup rewrite conditions. Here’s how you can rewrite URLs using mod_rewrite for Apache on Ubuntu 18.04

To begin with, .htaccess file is the backbone of mod_rewrite module. By placing it in the root folder of your website, you can easily rewrite URLs without even accessing server config files.

 

How to Rewrite URLs using mod_rewrite for Apache on Ubuntu 18.04

Here are the steps to rewrite URLs using mod_rewrite for Apache on Ubuntu 18.04

 

1. Install Apache

If you have already installed Apache, you can skip this step. Otherwise, you can install it simply using Ubuntu’s package manager apt


$ sudo apt update
$ sudo apt install apache2

This will update all system packages first, and then do the installation so that  everything goes smoothly.

 

2. Enable mod_rewrite

Although mod_rewrite is installed during Apache’s installation, it is disabled by default. You need to enable it with the command:


$ sudo a2enmod rewrite

Restart Apache server to apply changes


$ sudo service apache2 restart

 

3. Set up .htaccess file

.htaccess file allows you to define URL rewrite rules without altering server configuration.Before we modify it, we need to update server configuration (one time) to allow changes to .htaccess file.

You can do this by opening .htaccess file in a text editor


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

 

You’ll see <VirtualHost *:80> on line 1. Add the following lines within that block. Make sure the lines are properly indented.


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

. . .
</VirtualHost>

 

Restart Apache to apply changes


$ sudo service apache2 restart

 

Create .htaccess file


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

 

Add the following line at top of .htaccess file


RewriteEngine on

 

Save and exit the file

 

Ensure that .htaccess file is read-only for non-root users


$ sudo chmod 644 /var/www/html/.htaccess

 

4. Setup rewrite rules

By default, when a user visits a page, Apache looks for the exactly matching URL and nothing else. For example, if your website is example.com and you create a file about.html, then users will be able to visit example.com/about.html but not example.com/about. In the latter case, they’ll see a 404 : page not found error.

You can solve this problem by setting up a URL rewrite in .htaccess file.

Let us start by creating about.html file


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

 

Copy the following lines into it


<html>
 <head>
  <title>About Us</title>
 </head>
 <body>
  <h1>About Us</h1>
 </body>
</html>

 

You will be able to visit example.com/about.html but not example.com/about

So, open your .htaccess file


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

 

After the 1st line, add


RewriteRule ^about$ about.html [NC]

 

Your .htaccess file will look like


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

 

In the above statement, ^ indicates start and $ indicates end of URL, after the domain part. “about” is the pattern to be matched. [NC] is flag indicating the URL can is case insensitive.

So apache will serve about.html for :

  • example.com/about
  • example.com/about.html
  • example.com/About
  • example.com/ABOUT

But the following will give a 404 page not found error:

  • example.com/about/
  • example.com/product

 

Finally, restart Apache to apply changes


$ sudo service apache2 restart

 

Congratulations! You have set up URL rewrites using mod_rewrite for Apache on Ubuntu.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!