How to Enable mod_rewrite for Apache in Ubuntu


enable mod_rewrite for apache in ubuntu

Apache is one of the most popular web application servers that powers millions of websites around the world. Sometimes, while running a website, you might feel the need to rewrite URLs or setup URL redirects. This can be a tedious process if you try to it from scratch. Luckily, Apache provides a wonderful module called mod_rewrite that allows us to easily rewrite URLs in a clean manner. Here’s how to enable mod_rewrite for Apache in Ubuntu.

 

How to Enable mod_rewrite for Apache in Ubuntu

You can enable mod_rewrite for Apache in Ubuntu in just 4 steps.

 

1. Install Apache

You can skip this step if you have already installed Apache on your Ubuntu.

We’ll use the package installer, apt-get, to install apache2 to simplify our work. First, we’ll update the system package index with the command:

$ sudo apt-get update

This will update all the packages on your Ubuntu and prevent old/outdated packages from interfering with your installation

Next, we install apache2 with the command:

$ sudo apt-get install apache2

 

2. Enable mod_rewrite

mod_rewrite is also installed as a part of apache2. However, it is disabled by default. You can enable it with the command:

$ sudo a2enmod rewrite

This will enable the module or inform you that it is already enabled. Nevertheless, it’s essential to restart your apache server, with the command:

$ sudo service apache2 restart

 

3. Setup .htaccess file

.htaccess is a file where you store all the URL rewrite rules, without changing any server configuration files. The dot (.) before the file name ensures that it is hidden. It is an important file critical to the security of your web application.

Before we setup .htaccess file, we need to allow changes to it. You can do this by opening Apache’s configuration file using nano (in example below) or your favorite editor.

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

In that file, you’ll find a block <VirtualHost *:80> on line 1. Within that block, add the following code:

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

So, it will look like:

<VirtualHost *:80>
    <Directory /var/www/html>

        . . .

    </Directory>

    . . .
</VirtualHost>

 

Now, restart Apache

$ sudo service apache2 restart

 

Next, create .htaccess file

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

 

Add the following line to the top of .htaccess file

RewriteEngine on

Save and exit the file.

Since .htaccess is such a critical file, we need to ensure that it is read-only for other users. You can do this with the following command

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

 

4. Setup rewrite rules & other files

Let’s setup a simple URL rewrite which converts pretty URLs (www.example.com/about) to actual code (www.example.com/about.html). Typically, if you create a file titled about.html, users will be able to access example.com/about.html but not example.com/about. mod_rewrite allows us to overcome this problem.

We’ll start by creating file about.html

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

 

Copy the following lines into your html file

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

You will be able to access your_server_ip/about.html or example.com/about.html. However, if you try to access your_server_ip/about or example.com/about, you will get a 404 (page not found) error.

 

So we open our .htaccess file

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

 

After the first line, you can add:

RewriteRule ^about$ about.html [NC]

 

Now your .htaccess file will look like

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

 

In the above statement, ^about$ is matched in the URL. ^ is a metacharacter that indicates start of the URL after the domain name ends, and $ indicates the end. So the above statement means that when a user types a url example.com/about he/she should be served the contents of example.com/about.html.

[NC] tells Apache to ignore case of URL. So all the following URLs will point to about.html:

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

But the following will not:

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

 

Now, restart Apache

$ sudo service apache2 restart

 

Congratulations! Your users can now access example.com/about

 

 

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!