In this article and in the next we will explain how to use mod_rewrite, to map certain HTTP requests to other pages in a website, or to an external URL.

In other words, this well-known Apache module will allow you to redirect an URL to another, which we will illustrate through practical examples.

Suggested Read: 5 Tips to Boost the Performance of Your Apache Web Server

NOTE: The examples below assume you are at least somewhat familiar with Perl Compatible Regular Expressions (PCRE). Since that topic is out of the scope of this article, refer to the Perl 5 version 24.0 docs for more details on PCRE.

Before proceeding, make sure the rewrite module is loaded. Although this is the default behavior in CentOS and similar distributions, in Debian and derivatives you will need to load it manually as follows:

# a2enmod rewrite

Configuring Apache to Use mod_rewrite Module

For simplicity, let’s use the default site in a CentOS 7 box (IP 192.168.0.100) to explain how to use mod_rewrite (DocumentRoot: /var/www/html, configuration file: /etc/httpd/conf/httpd.conf).

In order for Apache to use this module, add the following line to the configuration file:

RewriteEngine on

It is important to note that this configuration will not be inherited by virtual hosts in the same box.

Thus, you will need to add RewriteEngine on for each virtual host where you want to use rewrite rules.

Internal redirection: mapping an URL to another in the same virtual host

An internal redirection is the simplest example of mod_rewrite. If you want to redirect all requests for default.aspx to index.html, add the following line (also known as a rewrite rule) under RewriteEngine on:

RewriteRule "^/default\.aspx$" "/index.html"

and don’t forget to restart Apache in order for the changes to take effect.

This may come in handy if your site was originally designed using ASP and later changed to plain HTML5. Search engines will have the .aspx file indexed but that file does not exist any more.

In that case, you will need to find a way to redirect the request so that your prospective visitors don’t run into an error page. To test, let’s create a simple HTML file named index.html inside /var/www/html with the following contents:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>New site</title> </head> <body> <h2>Default.aspx was here, but now it's index.html</h2> </body> </html>

The caret and dollar signs will cause the regular expression to match any string beginning with /default and ending with .aspx , respectively.

Suggested Read: Install Mod_Pagespeed to Speed Up Apache Performance Upto 10x

Now launch your browser and point it to 192.168.0.100/default.aspx. If things go as expected, Apache should serve index.html instead.

However, the end user will still see default.aspx in the address bar causing the change to be totally transparent:

If you want the URL in the address bar to show that the server it’s actually serving index.html instead of a page named default.aspx , add [R,L] to the end of the rewrite rule as follows:

RewriteRule "^/default\.aspx$" "/index.html" [R,L]

Here [R,L] are two optional flags that indicate that a complete HTTP redirect should be issued to the browser (R) and that no further rules should be processed:

Note how the address bar now shows index.html , as expected, instead of default.aspx as it did earlier.

Summary

In this article we explained how to use mod_rewrite to perform internal redirection. Stay tuned for the next post where we will learn how to redirect to a resource that has been moved to a different server, and how to display custom content based on the user’s browser. Until then, refer to the Apache 2.4 docs for a full list of the available rewrite flags.

As always, feel free to use the comment form below if you have any questions or feedback about this article. We look forward to hearing from you!