I remember a little while ago I wrote a post about hosts files, which are handy things for every web developer. Modifying your hosts files allows you to redirect requests to a given domain to a specific IP address. For example, you can have 16bit.orderinchaos.org redirect to your localhost (127.0.0.1) or another testing server, allowing you to test changes before changes are pushed to the real world. @lippertz recently posted a Windows hosts file manager on sourceforge. It’s a pretty simple and lightweight application. An icon lives in your system tray, on which you can click and then select which file you want from a pop-up menu. Remember – in most cases you’ll need to re-open your browser for the changes to take effect.
One challenge with hosts files is that you can only redirect to specific IP. I, for example, have a folder on my local for the files in davidwkennedy.com, which is c:\www\davidwkennedy. If I set my hosts file to redirect davidwkennedy.com to 127.0.0.1, this will work. However, I have a few parked domains within that folder. For example, if you goto blusun.us, it appears as though it’s own entity. In all reality, everything for blusun.us lives in a subfolder of davidwkennedy. Thus, on my local, it looks like c:\www\davidwkennedy\blusun. Thus, if I redirect blusun.us to 127.0.0.1, It will actually resolve the index page of davidwkennedy.com! There are a few approaches to this problem. One of which would involve configuring your server. Apache, by default, has a main ‘www’ folder, in which web projects live. Microsoft’s Internet Information Services, on the other hand, easily allows (and be defauly requires) you to configure domains and subdomains to resolve to specific local folders.
However, the approach that I took was to use a php file to inspect the url, and then redirect to a sub folder appropriately.
// This lives in my www directory.
// My hosts file contains the below entries:
// 127.0.0.1 davidwkennedy.com
// 127.0.0.1 blusun.us
<?php
function getURL
()
{
return $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
$url = getURL
();
switch ($url)
{
case ("davidwkennedy.com/"):
header("Location: ./davidwkennedy/");
break;
case ("blusun.us/");
header("Location: ./davidwkennedy/blusun/");
break;
default:
echo $url;
}
?>
View Comments
blog comments powered by