In the past we had the case where we
needed to add web redirection services for some friends..
I've been googling to find out some
ways to do so.. found only one sf.net .. but wasn't satified with it..
as it would require not using vegadns anymore..
So, one night, after long thoughs,
i've decided to ask two friends to find a way to, at first, to design a
script that would handle
any calls to it, and redirect to the
apropriate url, based on a simple file database.. Than, publish the
script, and see if someone
would be interested to add some
"redirection" field to vegadns interface and database, that would only
add the apropriate fields in the database of the
targeted domain..
After some tests, we came out with a
pretty simple script, that uses (for the moment), a simple text file to
figure "who goes where"..
In my further examples, i'll be
using this general idea:
Also note, as pre-setup on my "main"
company domain, i've created a generic alias that will be used as cname
for any url needed to be redirected..
Therefore: i made one Alias on my
main domain like : redirect.mydom.com > alias > 192.168.0.11
Than, in the zone of "domain.com", i
created : record some.url.domain.com > cname > redirect.mydom.com
The idea is that all
redirections have to point to redirect.mydom.com.
Now, the apache is setup on 192.168.0.11 :
one spare ip for apache is needed,
on my apache box, i'll use 192.168.0.11 here, with a very basic vhost
definition (provided below)
---cut---
<VirtualHost 192.168.0.11:80>
DocumentRoot /home/system/redirect/docs
ErrorLog /home/system/redirect/logs/redirect-error_log
CustomLog /home/system/redirect/logs/redirect-access_log common
</VirtualHost>
---end of cut---
At this point, all dns answers
redirect to redirect.mydom.com, and apache is setup to
use /home/system/redirect/docs/ as documentroot.
And now, the magic script:
---cut---
<?
// redirect.php version 0.3
// 27.11.04
//
// script created by Thomas Foncelle
(lobotom<at>lobotom<dot>org)
// based on ideas from Rudolph Sand
(r.sand<at>new-net<dot>net)
//
// edit this
// the file should be fully
readable/writable for apache..
$config_file = '/some/path/redirection.txt';
// don't touch anything else
$separators = Array("\t", " ");
if($_GET[debug]) {
echo('
<table border="1">');
} // fin if($_GET[debug])
$fp = fopen($config_file, 'r');
while ($data = "" 1024)) {
$line += 1;
if (strlen(trim($data)) && substr($data, 0, 1) != '#') {
unset($separator);
for ($i = 0; $i < sizeof($separators); $i++) {
if (strpos($data, $separators[$i])) {
$separator = $separators[$i];
} // fin if (strpos($data, $separators[$i]))
} // fin for ($i = 0; $i < sizeof($separators); $i++)
if (!$separator) {
die("<br>Erreur dans le fichier ".$config_file.":
separateur non trouve. (ligne ".$line.")");
} // fin if (!$separator)
else {
$tmp = explode($separator, $data);
unset($domain, $url);
$domain = trim($tmp[0]);
$url = "">
if ($domain && $url) {
$redir[$domain] = $url;
if($_GET[debug]) {
echo('
<tr>
<td>'.(($_SERVER[HTTP_HOST] ==
$domain) ? '<b>' : '').$domain.(($_SERVER[HTTP_HOST] == $domain)
? '</b>' : '').'</td>
<td>'.(($_SERVER[HTTP_HOST] ==
$domain) ? '<b>' : '').'pointe vers'.(($_SERVER[HTTP_HOST] ==
$domain) ? '</b>' : '').'</td>
<td>'.(($_SERVER[HTTP_HOST] ==
$domain) ? '<b>' : '').$url.(($_SERVER[HTTP_HOST] == $domain) ?
'</b>' : '').'</td>
<tr>');
} // fin if($_GET[debug])
} // fin if ($domain && $url)
else {
die("<br>Erreur dans le fichier ".$config_file.":
parametre manquant. (ligne ".$line." \"".$data."\")");
} // fin else
} // fin else
} // fin if (strlen(trim($data)) && substr($data, 0, 1) !=
'#')
} // fin while ($data = "">
fclose($fp);
if($_GET[debug]) {
echo('
</table>');
} // fin if($_GET[debug])
if (!$redir) {
die("<br>Aucune redirection trouvee dans le fichier
".$config_file.".");
} // fin if (!$redir)
if ($redir[$_SERVER[HTTP_HOST]]) {
if($_GET[debug]) {
echo('<br><br>Vous allez etre redirige vers
<b>'.$redir[$_SERVER[HTTP_HOST]].'</b> en cliduant <a
href="">ici</a>');
} // fin if
else {
header("location:
".$redir[$_SERVER[HTTP_HOST]].(($_SERVER[QUERY_STRING]) ?
'?'.$_SERVER[QUERY_STRING] : ''));
} // fin else
} // fin if ($redir[$_SERVER[HTTP_HOST]])
else {
die("<br>Aucune redirection trouvee pour le domaine
<b>".$_SERVER[HTTP_HOST]."</b>.");
} // fin else
?>
---end of cut---
Finally, here is the "database file"
wich of course should be replaced by proper calls to vegadns's
database..
warning: u must keep the first line
of file (#Domain Url) ...
---cut---
neptune# more
/home/www/radio-psylone/redirection.txt
#Domain Url
#here are infos for first redir:
#here are infos for second redir:
# etc...
---end of cut---
I have the setup running fine for over a month, with over 200
redirections in a .txt file
I'm providing this script with concent of Thomas.
We both agree it should be freely used by anyone needing such
services
But we both strongly request to be kept informed of further
changes made to the script.
Now the question is, would someone feel to add the sql calls to
vegadns' database ?
And provide modification to the forms, so another type of field
could be field : "REDIR"
Of course, such field could follow Len's sanity checking
policy.. Although the script already checks for a few commun mistakes..
Ah, final note, for those winXX users.. avoid NOTEPAD for
editing redirection.txt.. could save you some troubles..
Bye ;)