How to Perform Internal Redirection with mod_rewrite in Apache

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 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>
	<meta charset="utf-8">
	<title>New site</title>
	<h2>Default.aspx was here, but now it's index.html</h2>

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 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:

Mapping URL to Other Page on Domain
Mapping URL to Other Page on Domain

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:

Mapping URL to Actual Index Page
Mapping URL to Actual Index Page

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


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!

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.