Adding Sub-Domain Support to Your Free Dot TK Domain

by Trent Bradley  (a.k.a. Blue Collar Camel)

For those of you out there who are cheap like me, you may use Dot TK (www.dot.tk) to get a free top-level domain name.

While it is nice to have a free top-level domain name, the free version of the service was awfully limited.  It offered few email address forwarders and no support for sub-domains.

That's where PHP comes in.  By using the script I appended at the end of this article as your index file (presumably named index.php), you can add sub-domains to your website.

You do this by using the predefined PHP variable: HTTP_REFERER

The script looks at HTTP_REFERER and replaces http://, www, your domain name, .tk, all extra . (periods) and all extra / (forward slashes) with blank values.

It appends a / (forward slash) for picky servers.

It then sends a header back to the browser telling it to redirect to the folder that is specified by the sub-domain name (i.e., http://dl.downloadsite.tk redirects to http://www.yourwebsite.somefreehost.com/dl).

Whether or not you can do something like http://dl.downloadsite.tk/file.zip, I don't know.

You can find an updated version of this script, any questions that have been asked, and other articles/scripts at www.bluecollarcamel.net/articles

Script Setup

1.)  Insert the script appended to the end of this article to the top of your index page before the <html> tag.

2.)  Now rename the index file to: index.php

3.)  Change the $yourDomain variable to what your Dot TK domain is.  (Follow the instructions included in the script.)

4.)  Change the $baseURLvariable to what your base URL for the script is.  (Follow instructions included in the script.)

5.)  Open your Dot TK URL and test to see if the sub-domains work.

Still have problems?  Here are some possible solutions:

1.)  There isn't PHP support on your server.  If this is the case, you're out of luck unless someone ports it to another language (i.e., ASP.NET, Perl).  If you do port it, I would really appreciate it if you sent me a copy.

2.)  The script doesn't pick up the HTTP_REFERER thingy.  If this is the case, either you have set up PHP incorrectly or your host has set it up incorrectly or disabled it.  Also, some browsers (very few) don't support this.

<?php
/*
	DotTK Free Sub-Domain Script 1.02
	By Trent Bradley
	(C) 2005 Blue Collar Camel (http://www.bluecollarcamel.net/)

	Change log:
	1.02: Appends a "/" to the final redirect URL for the picky browsers/servers.
	1.01: Fixed a bug that didn't remove the extra "." from the entered URL.
            : Added the feature that (tries) to determine if it was a sub-domain that was entered.
	1.00: Initial writing.

    EDIT THESE VARIABLES ONLY!
 
    Your actual DotTK domain. Do not include the ".tk", "http://", or "www."
    Example: if your full URL was "http://www.downloadsite.tk/", you would put "downloadsite".
      $yourDomain = "yourdottkdomain";
 
    The base URL for this script.
    Example: if the full URL to the script was "http://youraccount.freehost.com/thisscript.php",
    you'd put "http://youraccount.freehost.com/". You MUST include the last "/"!
      $baseURL = "http://youraccount.freehost.com/";
*/ 

 // Get the entered domain name (by a visitor, you, etc.)
 $fullDomain = $HTTP_SERVER_VARS['HTTP_REFERER'];

 // Replace the "http://" with a blank value in the entered domain-name
 $redirectPath = str_replace("http://", "", $fullDomain);

 // Replace the "www." with a blank value in the entered domain-name
 $redirectPath = str_replace("www.", "", $redirectPath);

 // Replace your $yourDomain with a blank value in the entered domain-name
 $redirectPath = str_replace("$yourDomain", "", $redirectPath);

 // Replace the ".tk" with a blank value in the entered domain-name
 $redirectPath = str_replace(".tk", "", $redirectPath);

 // Replace all "."'s with a blank value in the entered domain-name
 $redirectPath = str_replace(".", "", $redirectPath);

 // Replace the (possible) end "/" with a blank value in the entered domain-name
 $redirectPath = str_replace("/", "", $redirectPath);

 /* 
    Determine if the URL is a sub-domain. If the $redirectPath variable is blank, it means that this is NOT a sub-domain.
    Note: This can easily be fooled by appending text to the end of the URL.
    Example: "http://www.downloadsite.tk/foo"
    That would cause the script to try and redirect to "http://youraccount.freehost.com/foo"
 */
 if (strlen($redirectPath) > 0) {
  // Append the final redirection path to the base URL
  $redirectPath = $baseURL . $redirectPath . "/";

  // Redirect the (yours, visitor's, etc) browser to the actual location.
  header("Location: $redirectPath");
 }
 else {
  // If the URL isn't a sub-domain, the script simply displays your original index page. 
 }
?>

Code: dottk.php

Return to $2600 Index