Dynamic Site Cloaking and Redirecting
From Charlie.Huggardlee.Net
Of the many preparations for Tiffany and my big day coming up in June, we wanted to help our relatives and guests by providing them with a wedding website of useful information. Initially our wedding website consisted only of a wedding map so that using Dreamhost's subdomain cloaking option was sufficient. But as we had to add registry info, and potentially will be archiving additional information after the wedding (e.g. pictures), I needed a simple system to provide easily configurable path level cloaking. In this article I discuss the components of a system I developed for dynamic cloaking and redirections.
MySQL Table
As I had a mySQL server readily available, I decided to use a mySQL table for the backing source for this system. Admittedly this system can be retooled to use any other form of backing datasource (flat files, XML, etc). This simple system consists of 1 table defined as follows (with some example data);
DROP TABLE IF EXISTS dscandr; CREATE TABLE dscandr ( server VARCHAR(48) NOT NULL, request_uri VARCHAR(48) NOT NULL, url VARCHAR(256) NOT NULL, title VARCHAR(255) NOT NULL, PRIMARY KEY (server,request_uri) ) ENGINE=MyISAM; INSERT INTO dscandr (server, request_uri, url, title) VALUES ('*', '/', 'http://example.com', '*'), ('redir.example.com', '/', 'http://redir.example.com/index', '*'), ('redir.example.com', '/index', 'site.htm', 'Example.com Redirect Server'), ('redir.example.com', '/map', 'http://maps.google.com/', 'An Interesting Map');
PHP Script
The meat of this system is this script. If it encounters a request for an unknown path, it'll fall back first to the root record (request_uri='/') of the current server, then to the overall default entry (server='*', request_uri='/'). If the title field is set to '*', this is interpreted as a path that needs to be redirected to the specified url, whereas if a title is specified, this is interpreted as a path that needs to cloak the specified url.
<? $link = mysql_connect("mysql.example.com","username","password") or die('Could not connect: ' . mysql_error()); mysql_select_db("databasename") or die('Could not select database'); $server = strtolower($_SERVER['SERVER_NAME']); $ruri = mysql_escape_string(strtolower($_SERVER['REQUEST_URI'])); $result = mysql_query( "SELECT * FROM dscandr WHERE server='$server' AND request_uri='$ruri' ". "UNION SELECT * FROM dscandr WHERE server='$server' AND request_uri='/' ". "UNION SELECT * FROM dscandr WHERE server='*' AND request_uri='/' "); if(!$result) { mysql_close($link); die('Error executing query'); } $row = mysql_fetch_assoc ($result); mysql_close($link); if ($row['title']=='*') { header("Location: ".$row['url']); exit; } ?><html> <title><?=$row['title']?></title> <frameset rows="0,*" framespacing="0" border="0" frameborder="0"> <frame scrolling="no" noresize> <frame scrolling="auto" noresize src="<?=$row['url']?>"> </frameset> </html>
.htaccess Magic
As we want almost every request for a particular server to be handled by our php script above, this involves a little bit of .htaccess magic (quite similar to that used for Beautifying Mediawiki URLs). We redirect everything except requests for existing files and directories.
RewriteEngine On RewriteRule ^$ dscandr.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ dscandr.php [L]
Case Study
wedding.huggardlee.net is Tiffany and my wedding site using this scheme and has the following rules in place (as of 27 Jan 2007).
- http://wedding.huggardlee.net/ & any other non-existant url
- Redirection to http://wedding.huggardlee.net/map
- http://wedding.huggardlee.net/index
- Cloak of our wedding site index
- http://wedding.huggardlee.net/map
- Cloak of our wedding map on Wedding Mapper
- http://wedding.huggardlee.net/registry
- Cloak of our wedding registry at Macy's
--Charlie Huggard 21:08, 27 January 2008 (CST)
or read what others have said
| Author | Charlie Huggard + |
| Post date | 28 January 2008 03:08 + |
| Read more | true + |

