|
Perl Case Study - Unlimited Subdomains CGI What is a subdomain? When you surf the web, you often see URLs like http://www.yoursite.com. In addition to that, you may see ones like http://games.yoursite.com. Instead of www, you have "games." This entire URL is a subdomain and "games" makes it unique from http://www.yoursite.com. URL redirection services such as CJB.Net give a subdomain by letting you vary the variable in the URL http://variable.cjb.net. Because even subdomains - which would normally be expensive because they point to unique IP addresses (which in turn, point to your server). However, I learned a trick which allows you to have unlimited subdomains by taking advantage of the $ENV{'HTTP_HOST'} variable (see Perl Case Study - Environment CGI).
This might be useful to you if you have: How can you tell if you have wildcard subdomains enabled? Just type in any subdomain to your domain name. If something like http://perl.yoursite.com loads the same page as http://www.yoursite.com, then you have wildcard subdomains enabled (try several different subdomain possibilities to make sure). What about Server Side Includes (abbreviated as SSI)? Create a text file (and put anything you'd like to in it, like "hello." We'll thus call it hello.txt. Then create a HTML page and put the following tag in the body: <!--#include file="hello.txt"--> Put these both in the same directory/folder on your server, then try to view them in your browser. If the browser printed out something like ... [an error occurred while processing this directive] ... or nothing at all (!) your server is not SSI enabled. If you're still with me, let's start on the script algorithm.
#!/usr/local/bin/perl
#This will return the URL after http://
#Notice that there are three parts to the URL
#Use a split function to separate this $hostname into its three
parts
#Now we have:
#This will essentially be redirection. If you have not already
read
#Depending on what $parts[0] is, you will redirect the visitor to
a
#Let's store the specified redirection URLs in a separate file. I'll
open(URLS,"urls.txt");
foreach $pair(@urls) {
Since this server doesn't allow wildcard subdomain names, we don't have an example installed to show you, but after consulting an Apache server administrator, I've been told that it is very simple to modify an Apache file on the server to enable this feature: "It's a matter of prepending one line in the DNS entry by two characters, then adding another line in your domain's section of the apache config." - aka Spock. How do you implement this script? No your users will not have to type in the url to call the script directly (that would defeat the purpose of the subdomain-based redirection). Remember how I said that any subdomain would go to the index page on your server (index.shtml)? Well, if you have server side includes, you cound literally embed a call to execute the cgi script right in your index page! Your index.shtml page should consist of one line: <!--#include virtual="subdomains.cgi"--> And that's it. When the page (index.shtml - .shtml is the most common extension for server side include enabled pages but whatever the name of the default page that loads should work as long as it allows SSI) loads, the $ENV{'HTTP_HOST'} will capture the subdomain name that was entered in the browser to reach the page and you're on your way :) As for the modification I mentioned in the script above, you have the option, in your urls.txt file to specify the file on your server to show. Instead of redirecting your visitors, just open the file:
open(URLS,"urls.txt");
foreach $pair(@urls) { print "Content-type: text/html\n\n";
open(PAGE,"$file"); foreach $line(@page) { print "$page\n"; }
last This snippet of code in place of the redirection code will instead open a local file and return the contents of the page directly into the index.shtml file for display (this takes greater advantage of the server side include "dynamics" of index.shtml). Can it capture the HTML of a page on someone elses server? Yes, but we haven't gotten around to learning this neat little function yet. Speaking of which ... check out the next case study :) Last Updated August 11, 1999
|
||||||||||
©1999 Team 26297 "Ad Infinitum Web." All rights reserved. Any reproduction of this document for commercial or redistribution purposes without the permission of the author is forbidden.