Redirect The Rage

Last night I was doing some contract web work, and discovered just how easy it is to put some redirection into place. I realise this will be bread & butter knowledge to most webfolk.

If you have more than one (or maybe even just one) domain name that is pointing to the same IP, and you wish to redirect the user based on the address they typed, there is a simple way.

Following is the code that I knocked up last night. It works for files in the same directory (the include), and for places outside (the header redirect).

One NUB mistake I made was putting my initial comments in html comment at the top. Bad Bad Bad! The header function has to come before any html. Man alive. No0b. Newb.

< ?php

/*************/

/* index.php */

/*************/

/* Created: 06-04-19 Purpose: This is the Redirect file. It simply processes the domain name the user has entered, and redirects accordingly. */

/* Get the "Domain Name" */

$ServerName = $_SERVER[‘SERVER_NAME’];

/* Redirect to main Index */

if (strstr($ServerName,"mysite001.com") || strstr($ServerName,"mysite001.com.au")) { include(‘index.html’); }

/* Forums redirect */

elseif (strstr($ServerName,"mysite002.com")) {

//include(‘http://www.mysite002.com/forum/index.php’); /* this doesn’t work, needs a physical redirect rather than the include */

header("Location: http://www.mysite002.com/forum/index.php"); /* Redirect browser */

}

/* CatchAll … this doesn’t have to be the main Index, but is for the moment */

else { include(‘index.html’); };?>

There you have it. Not much to it. This assume that apache (or whatever server) will take a gander at the index.php file first. If it doesn’t, you can either change the way apache looks (want to put the .php first in the http.conf file (i think that’s the one from memory), or just don’t have any other index. files … if you have an index.html, change it to indexmain.html .. or something along those lines.

Cheers, thanks for listening, you’ve been a wonderful audience,