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!

Gabriel Cánepa
Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 Comment

Leave a Reply
  1. How would I redirect internally from this URL:

    to this:

    i.e maintain the rest of the URL params and add .php extension where the URL starts controller.


Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.