How can I make subdomains redirect to URLs like test.mydomain.com goes to http://www.someotherwebsite.com

Ed Shnatter
Ed Shnatter used Ask the Experts™
on
Is there an easy way to set up shortcuts using your own domain (but not break or involve the existing website)?

Say the domain is mydomain.com.  People currently can type mydomain.com or www.mydomain.com and it goes to the website.

I'd like to be able to set up shortcuts / redirects so  something like time.mydomain.com would redirect to a specific website (in this case, maybe www.time.gov).

This is for someone else.  A long time ago I found a perl script that I put on my website, called it index.cgi, and it had this info:

#!/usr/local/bin/perl       
$mainpage       = "index.html";
%domains = (      
'www.mydomain.com'          =>  'https://www.mydomain.com',      
      
#enter shortcuts here      
'time.mydomain.com'         =>  'http://www.time.gov',


#end of shortucts

'www.mydomain.com'          =>  'http://www.mydomain.com/index.html'      
      
);      
$server = lc($ENV{'HTTP_HOST'});if ($ENV{'HTTP_HOST'} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/){$t='/'}else{if ($server !~ /^www./){$server = '' . $server;}}@domains = keys(%domains);foreach $domain(@domains){if ($domain eq $server){print "Location: $domains{$server}\n\n";$match="yes";last;exit}}unless($match){print "Location:  https://www.mydomain.com/index.html\n\n";exit}exit;      
      
 
That does what I'm asking about - enter loads of shortcuts and you can enter that subdomain and it takes you to the appropriate webpage.

Are there other ways via the DNS zone record or something else?  

Are there simpler ways to do this redirect of subdomains to specific URLs
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sign up for a DNS provider that supports web redirects.  Any cPanel host provider managing DNS would work.

If you need some direction, check out https://www.cloudns.net/

Once you have your account, and have modified your name servers to point to ClouDNS, you can simply create a "Web Redirect" record and specify the host (i.e. time.example.com), the redirection location, and whether or not you want to redirect within an iframe or just a regular header location redirection (301 Moved permanently or 302 Temporary redirect).

The actual DNS records are handled on the back-end and ClouDNS hosts redundant DDoS protected web servers to host the actual redirection code.

That being said, if you want to do your own hosting the perl script above could be boiled down to the simple code below, where you'd place a separate index file in each directory for each host name you create (although definitely not as efficient of a solution.)

print "Location: http://www.example.com/\n\n";

Open in new window

Author

Commented:
Thanks for the info!  It opens another can of worms / questions : )

DNS queries - that includes each email, not just people going to websites, right? Mail servers have to look up the mx?

And talking of email. lets say the domain is using office 365.  How does cloudns do mail forwarding then?
You’ll need to access and copy your current DNS records, then recreate them as needed in ClouDNS.  If you do this before you switch name server providers, the transition will be seamless.  So yes, definitely include MX records, SPF TXT records, and anything else existing in your current zone file.  If you can export your current DNS zone BIND formatted file, ClouDNS has an import option.  For your host name entries that need web redirects you’ll omit those (CNAME, A, etc.) and use “Web Redirect” records instead.

For Office 360, you’ll want to verify all these records are present before switching over: https://docs.microsoft.com/en-us/office365/admin/get-help-with-domains/create-dns-records-at-any-dns-hosting-provider?view=o365-worldwide
David FavorFractional CTO
Distinguished Expert 2018

Commented:
You asked, "Is there an easy way to set up shortcuts using your own domain (but not break or involve the existing website)?"

Yes, there are 2x ways to do this.

1) Use a Webserver .htaccess or PHP redirect approach.

2) Use DNS, which will likely be easier to setup + manage, so an example DNS record...

time  IN  CNAME  www.time.gov.

Open in new window


Be sure to append the trailing "." for correct resolution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial