buzzword compliance

A Three-way homepage switch in PHP

Since I've been consolidating hosting of some of my domains, I ran into a little bit of a problem; I had three domain names pointed at the same host, and I wanted each of the domains to have its own home page. So, 10 minutes of PHP coding later, my problem was solved.

I inserted a simple redirector based on the $HTTP_HOST and $REQUEST_URI server variables in PHP, which capture the URL that the user has requested. The rest is just sending standard HTTP redirect headers, so that the user will be forwarded to the page they've come looking for.

I reused a code-snip from an earlier project that creates a string variable that stores the fully-expressed URL of the site, just in case I decide to add some other options later.

The switch uses the stringsearch function “strstr” to find the text for which domain the incoming request is pointed at, then sends back a header to redirect users to the corresponding directory for the homepage of that site. Extending this, I can now on a single host maintain as many domains as the provider will let me point at the site, and as storage will allow.

To build a home page that redirects based on the incoming URL, (if your site supports PHP), use this code and substitute your own domain names. If you've got more than three, just repeat the “if” statements as many times as you need to. (It's not pretty, but it works.)

<html>
<head>
<?php
$url = sprintf("%s%s%s"," http://&quot;,$HTTP_HOST,,br>
$REQUEST_URI );
if (strstr($url, "INSERT DOMAIN NAME #1 HERE")) {
?>
<title>Forwarding to INSERT DOMAIN NAME #1 HERE</title>
<meta http-equiv="Refresh" content="1; URL=http://DOMAINNAME#1.com/DIRECTORYOFHOMEPAGE/">
<?
}
if (strstr($url, "INSERT 2ND DOMAIN NAME HERE"))
{
?> <title>Forwarding to 2ND DOMAIN NAME Home</title> <meta http-equiv="Refresh" content="1; URL=http://www.2NDDOMAINNAME.com/DIRECTORYOF2NDHOMEPAGE/">
<?
}
else { ?>
<title>Forwarding to 3RD DOMAIN homepage</title>
<meta http-equiv="Refresh" content="1; URL=http://www.3RDDOMAIN.com/index2.html">
<?}
</head>

Standard

Leave a comment