Redirect subdomain.example.org to somethingelse.com hidden to the user?

I'm not knowledgeable at all about C-NAMES, A-RECORDS, name servers and the like, so just bear with me on this one:

I want to redirect subdomain.example.org to another domain somethingelse.com. I do own both domains and both domains run on shared hosting. When creating a subdomain, all I can do out-of-the-box is do a HTTP redirect (302, yuck!) or a frame redirect.
I also can set the DNS records on one host like nameserver, CNAME - but I would have know clue what I am doing.

Please explain to me how routing with nameservers works and how I should (even if not possible with my current host) redirect subdomain.example.org to somethingelse.com with the user-visible URL staying at "subdomain.example.org"


Solution 1:

Name servers do not route anything. Name servers respond to requests for names, and they eventually returns an IP. CNAME records return a name, which is then looked up to get the IP. The only thing you really can do with your DNS records is make sure that your name is set returns the IP address of a server that will accept the traffic and do what you want.

If you setup the web server with name-based virtual hosting setup, and the main name for that virtual host is somethingelse.com then you could set an additional name for that virtual host subdomain.example.org. In DNS you set the records to return the IP of the virtual host serving somethingelse.com.

The web site hosted on somethingelse.com must used relative links wherever possibly or the user will quickly realize that they are now have been switched to a different domain.

... this is the crucial point. How would I do that?

Just create an A record that associates a name with an IP, or CNAME record that points to an A record that points at the IP you want.

DNS knows nothing. What you are asking for needs to be handled on the HTTP server.

Otherwise I could direct my domain at google.com and make it look like it's my site?

You can point a DNS record at any IP address you like. In the case of HTTP, part of the request includes the host name you are trying to access. The Google web servers may choose to reject based on the hostname that is part of the HTTP request.

Setting up a DNS name to point at Google's servers is even supported if you are a Google Apps customer. There is a procedure you most follow to tell Google what name you will be using for a particular page. Google then adjusts their configuration accordingly.

If you are using some other protocol that doesn't include the hostname used to access then the server would have no way of knowing what is what. You can easily test this for yourself by creating a record like googledns.example.org IN A 8.8.8.8, and then running ping googledns.example.org, Google's DNS server will respond to the ping. The name you used to for the ping command is not included in an ICMP echo reply at all, only the IP address.

Solution 2:

Why can't you just setup a .htaccess file (with a 301 redirect) in the folder that the subdmain is in.

For Example:

  1. Create a .htaccess file in the folder example.org/subdomain
  2. Setup your subdomain in Cpanel based on the above folder
  3. Edit that new .htaccess file and add the following code

    Redirect 301 / http://somethingelse.com

With / being the root directory of your subdomain, and http://somethingelse.com being where you want it sent.

Works for me.

Solution 3:

DNS redirects using CNAMES won't work the way you want them to on a shared host.

For our purposes, suppose you have a 'CNAME' record for subdomain.example.org that point to somethingelse.com and an 'A' record for somethingelse.com. When you visit subdomain.example.com, a DNS server is first queried to determine the correct IP address. The DNS server will see the CNAME record and recursively determine the IP address for somethingelse.com, which is then returned to your desktop.

Your browser will then contact that IP address, it it will ask for subdomain.example.com. In fact, it doesn't even know somethingelse.com exists.

If you had a dedicated box, you could get CNAMEs to work like you think they should, but you would forfeit the ability to use a name-based virtual host. With IP-based vhosts, any domain that points to a given IP will serve the same content.

To get your browser's address bar to say subdomain.example.org while the page is being served from somethingelse.com, you may reconsider using the frame redirect provided by your host.

Another possibility, if your host provides mod_proxy, is to set up a reverse proxy in subdomain.example.com's virtualhost configuration:

ProxyPass / http://somethingelse.com/
ProxyPassReverse / somethingelse.com/

This will cause subdomain.example.com to provide somethingelse.com's content while still reporting subdomain.example.com as the source domain.