Introduction

The Apache mod_rewrite module is commonly used to implement URL rewriting and redirection rules. It is also required for .htaccess files to function correctly when URL rewriting rules are configured at the directory level.

This document explains how to enable the mod_rewrite module on Apache 2.4 running on Ubuntu, configure .htaccess support, restart Apache, and verify that the module is working correctly.

Prerequisites

Before proceeding, ensure the following requirements are met:

  • Root or sudo access to the Ubuntu server.
  • Apache 2.4 is installed and running.
  • The website’s document root is known.
  • The required .htaccess file and rewrite rules are available.
  • A backup of the Apache configuration is recommended before making changes.
  • Ensure that the Apache configuration is valid before restarting the service.

IMPLEMENTATION

Mod rewrite is an essential module for .htaccess rules to work on apache server. Follow the below post incase apache is not installed on host.

Install Apache 2.4 on Ubuntu

1. Login to the server as root.

2. Execute the below command to enable the mod_rewrite module.

a2enmod rewrite

3. Restart apache service to make the effect.

service apache2 restart

If you followed the above post, there will be a default configuration 000-default.conf. Open the file and add the following content.

vi /etc/apache2/sites-enabled/000-default.conf

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

If the documentroot is different other than /var/www/html then replace it with appropriate directory.

Restart apache service for the changes comes into effect.

service apache2 restart

Verification:

1. Execute the below command and look for rewrite module in the output.

apachectl -M|grep rewrite

2. Create a .htaccess on the directory and add simple rewrite rule.

vi /var/www/html/.htaccess

RewriteEngine on

Access the domain or IP address (http://IPAddress) on the browser. If you see 500 internal error then there is a problem with rewrite module.

Look for any error while accessing the page on browser using below command. If you see any error related Rewrite  then there is a problem with rewrite module.

tail -fn0 /var/log/apache2/error_log

Incase of any issue then please comment here.

Conclusion

The mod_rewrite module is required when Apache needs to process URL rewriting rules through .htaccess or the Apache virtual-host configuration. Enabling the module with a2enmod rewrite, setting AllowOverride All for the appropriate document root, validating the Apache configuration, and restarting Apache ensures that rewrite rules can be processed correctly.

After implementation, always verify the module using apachectl -M | grep rewrite and test the website while monitoring the Apache error log for any configuration or rewrite-related errors.

Leave a Reply