Apache Redirect/ Rewrite /alias

Published by

Posted on December 06, 2007

This invokes a php script that is five directories beneath Apache’s DocumentRoot.
It then passes a variable, within the script, the value “blahblah”. With url redirection, you could configure Apache to redirect
the following url to the one above:


Even with this simple example of redirection, this improves the integrity of the website in the following ways:

* The url is not so complex now and is therefore easier to remember.
* Using redirection in such a way avoids many security risk related to cgi and php scripts, etc… (See *)

* Say someone notices your passing varible values to the script from within the url. This person
may deviously find a way to pass and unexpected value to your script and cause unexpected
behaviour. For example:


Here, for example, someone has tricked your script into running some mailicious code somewhere
causing who-knows-what kind of behaviour and damage!!!

Types of Redirection

There are three main modes of redirection in Apache. These are the Alias, Redirect and Rewrite directives. The first two are defined in mod_alias.c within the Apache source and are compiled into Apache by default. Rewrite is defined in mod_rewrite.c, which is a much more robust module than its counterparts, however it is not compiled into Apache by default.


Alias /something /somewhere/else/in/the/filesystem

The Alias directive allows you to redirect “/something” to somewhere in the filesystem that is underneath the defined DocumentRoot.
For example, we use the following:

Alias /~labstaff /blue/homes/labstaff/www-home/

Therefore, “http://www.cs.utk.edu/~labstaff” is actually redirected to the directory “/blue/homes/labstaff/www-home/”.
We do this with “/~labstaff” and not “/~help” because “help” is a valid user and “labstaff” is not.


Redirect /something http://some.other.url.com

This directive will map “/something” to another url completely. This url can be any valid url of you choosing. For example, the following could be used if we wanted:

Redirect /rt http://rt.cs.utk.edu

This would map “http://www.cs.utk.edu/rt” to “http://rt.cs.utk.edu”. If you wanted to map a user’s webpage directory to their webpage elsewhere, for example, you could do something similar to:

Redirect /~sammons http://web.utk.edu/~csammons

This directive can be placed anywhere in the Apache configuration, so long as it is contained within a section.

NOTE: One difference between Alias and Redirect is that the browser is aware of the new location with Redirect, but is not with Alias.
Hence, the new location will be used as the basis for links with Redirect, but not Alias.


The beauty of Rewrite is that it can perform all of the above operations and more.

RewriteEngine on
RewriteRule RegExPattern /url/to/redirect/to/goes/here [Flag(s)]

\_ Pattern _/ \___ Substitution String ___/

One thing that makes mod_rewrite such a valuable tool is its ability to utilize regular expressions in both the Pattern and the refrences
in the Substitution String. Now, take the following rewrite rule for example:

RewriteRule ^/[~]cs530/hw(.*)$ https://%{SERVER_NAME}/~cs530/hw$1 [R]

\___ Pattern ___/ \_____ Substitution String _____/ \_ Flag _/

In this case, we want to match anything referenced on our server from the “/~cs530/hw” directory. Hence, all of the following would match:


The ‘$1’ on the end of the substitution string will be replaced by whatever trails the ‘hw’ in the pattern, as indicated by the ‘(.*)’.
Therefore, the above urls would be substituted with the following urls respectivly:


What’s the difference? We have changed the protocol being used from http:// to https://, thus forcing ssl (an encrypted connecion)!

Notice the use of the Apache variable “SERVER_NAME” in the substitution string. In fact, Rewrite is capable of using many of the other Apache variables in similar ways.