Published by exdone
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:
http://www.website.com/stuff
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:
http://www.website.com/lots/…/script.php?variable=/path/to/malicious/code.php
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*
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*
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
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.
*Rewrite*
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:
http://www.cs.utk.edu/~cs530/hw
http://www.cs.utk.edu/~cs530/hw/
http://www.cs.utk.edu/~cs530/hw/stuff.html
http://www.cs.utk.edu/~cs530/hw/some/other/directory
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:
https://www.cs.utk.edu/~cs530/hw
https://www.cs.utk.edu/~cs530/hw/
https://www.cs.utk.edu/~cs530/hw/stuff.html
https://www.cs.utk.edu/~cs530/hw/some/other/directory
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.
[http://www.cs.utk.edu/~sammons/docs/redirect.php|Reference]