10

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"

4 Answers 4

9

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.

2
  • "In DNS you set the records to return the IP of the virtual host serving somethingelse.com." <-- this is the crucial point. How would I do that? I can imagine that it shouldn't be possible to just "direct my domain at some other IP"? Otherwise I could direct my domain at google.com and make it look like it's my site?
    – Dennis G
    Feb 1, 2011 at 21:15
  • The problem with A records is cloud hsps. Its becoming the norm. I have to figure out a way to redirect subdomain.domain.com to new.domain.com making it look seemless and even passing cookie values. Feb 7, 2017 at 5:48
4

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.

1
  • 3
    I could, as I also have written in the OP. This doesn't hide anything though as the redirect is still seen by the user. The only true way is to use DNS to map the subdomain to the other domain.
    – Dennis G
    Nov 25, 2012 at 12:45
2

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.

1
  • Yes, that's what I was afraid of being on a shared host as the other server doesn't even have a dedicated IP address as many hosts share the same one... In the end the frame-based redirection is nothing else than a html page with frame src set to the other domain - that I can do with iframe as well and and run into cross-domain problems (e.g. Internet Explorer not accepting cookies)
    – Dennis G
    Feb 1, 2011 at 23:42
1

The only complete solution is to setup a web server to listen for 'subdomain.example.org' and redirect it to 'somethingelse.com' with HTTP redirect.

If you use a cname to point 'subdomain.example.org' to 'somethingelse.com', the web browser will talk to the web server for 'somethingelse.com' but it will say "I'm looking for subdomain.example.org" which it may not be configured for. If you can configure the web server to accept both URLs, then cname will work fine.

1
  • Please, some more insight. When configuring my CNAME on subdomain.example.org->somethingelse.com - all I would need is to have somethingelse.com to accept this CNAME? What's that called? A-Record, a nameserver change? What is actually needed...
    – Dennis G
    Feb 1, 2011 at 20:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .