Rewriting URLs on the Fly

This is more of a note to myself and is not Flash related, but it might be useful to someone out there. Bottom line is that I want to know where to find this info later on, for proper documentation. Anyway, I needed to replicate the experience you get when accessing a redirection system like the ones used by the PHP online manual (try http://www.php.com/mysql_query) or Macromedia’s ‘go’ pages (try http://www.macromedia.com/go/flex).

I found out this could be done directly from Apache, using mode_rewrite, a module write by Ralf Engelschall and available on Apache versions later than 1.3. Here you will find detailed information about how complicated processes does mod_rewrite relies on to provide extremely flexible and powerful URL manipulation mechanisms… and here you will find a guide to some popular uses for mod_rewrite. Both are highly recommended readings.

But if you are in a hurry, and would just like to have a solution for simple redirections like the examples mentioned, all you need is to follow this steps:

First, we create the php file that would create the redirection. For sake of simplicity and to demonstrate the point of this example, we will just print the filename request:

/your/docroot/go/index.php :

<?
echo $QUERY_STRING; // we could request a real document using this data
?>

Then, we create a new .htaccess file on the directory you will be using for redirection, in this case /go:

/your/docroot/go/.htaccess :

RewriteEngine on
RewriteCond /your/docroot/go/%{REQUEST_FILENAME} !-f
RewriteRule ^(.+) http://your.webserver.dom/go/index.php?$1

That’s it! Now, whenever you make a request to your ‘go’ directory, index.php will print the last part of your url request. For example, http://www.digitalmediaconcepts.com/go/something.

Using some instructions we could redirect queries to specific documents, or send every failed request to a different web server. Very handy.

April 6, 2004

Posted by: Oscar Trelles

Category: Uncategorized

Tags:

Comments

No Comments

Leave a reply

Name *

Mail *

Website