Breaking a domain

Posted on 1999-12-22
Medium Priority
Last Modified: 2010-03-05

I am starting a redirection service on some of my domain names.  It uses Wild Cards domains and when someone types anything.mydomain.com the script is run.  It gets the $ENV{'HTTP_HOST'} and stores it into $url.  

How can I 'break' it so the $url value is broken into 2 parts and stored.  So the value in $url would be


The 'therename' part is stored in $username and the 'mydomain.com' is stored in $address.

Any ideas?  Thanks in advance!  Happy Christmas to everyone!

Graeme Sandwell
Question by:graemesandwell

Accepted Solution

frugal earned 400 total points
ID: 2302186
I quickly hacked up something here. Bear in mind that I'm not that great with regular expressions so if you're a whiz with them you might be able to get it a bit simpler but this works. Read the comments for more info on how it works. I hope this is clear!


# get the domain, this would be coming
# from somewhere else in your script.
$domain = "my.domain.com";

# the split function uses the regular
# expression specified (in this case .
# which I had to escape because it's a
# reserved character in reg exps) as a
# seperator and spits out the rest as an
# array of values. In this case the first
# value goes to the $host variable. The
# rest goes to an array, @domain.
($host,@domain) = split(/\./,$domain);
# join does the reverse of split, it
# joins an array into one string using
# the specified character as a seperator # (in this case . again).
$domain = join(".",@domain);
# print the output, in your case I assume
# this would actually be a redirect.
print "Host is $host\n Domain is $domain\n";
LVL 85

Expert Comment

ID: 2302282
#assuming $host is the part before the first .
($host,$domain) = split(/\./,$domain,2);

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question