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 ( 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., redirects to

Whether or not you can do something like, I don't know.

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

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.

	DotTK Free Sub-Domain Script 1.02
	By Trent Bradley
	(C) 2005 Blue Collar Camel (

	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.

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

 // Get the entered domain name (by a visitor, you, etc.)

 // 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: ""
    That would cause the script to try and redirect to ""
 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