Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have several sub-domains on 1 website

http://sub1.mysite.com
http://sub2.mysite.com
http://sub3.mysite.com

I've also registered equivalent domains for each of these sub-domains at

sub1.com
sub2.com
sub3.com

How can I set it so that when someone types the sub1.com domain, they see the content from http://sub1.mysite.com but still see sub1.com as the URL. So the content is really served from http://sub1.mysite.com without the visitors knowing that.

share|improve this question
add comment

migrated from stackoverflow.com Sep 7 '10 at 2:01

This question came from our site for professional and enthusiast programmers.

4 Answers

This is kind of multi-layered.

Firstly, you need to setup a CNAME record for sub1.com to point to sub1.domain.com. This is done via whoever is hosting the DNS for sub1.com. You should more than likely have a control panel at either your hosting company or your domain name registrar to add this.

All this does however is tell clients that your website sub1.com lives at a certain IP address (i.e. in the case of a CNAME, it says, look up the IP of sub1.domain.com and sub1.com lives at the same location.)

Secondly, you need to tell your webserver that sub1.com and sub1.domain.com are the same site.

For Apache, you probably already have a <VirtualHost> setup, so you would add in a ServerAlias directive to tell Apache that the VirtualHost also serves sub1.com as well as sub1.domain.com

... signifies that you have your own configuration directives in here already.

<VirtualHost ...>
    ServerName sub1.domain.com
    ServerAlias sub1.com
    ...
</VirtualHost>

Thirdly and lastly, you need to make sure that your site doesn't redirect or reference sub1.domain.com within the code (i.e. Header redirects through PHP, meta tag refreshes, or a href links within the HTML to redirect the user outside of the current domain).

Links should have absolute paths without the domain, e.g. 1 not 2

1.

<a href="/blah.html">Blah</a>

2.

<a href="http://sub1.domain.com/blah.html">Blah</a>
share|improve this answer
add comment

You need to have the domains point to the same server of the subdomains, and using your hosting control panel set the domains as aliases to the subdomain.

p.s. asking those questions on http://serverfault.com/ gives you much better feedback, since that is the place to ask such questions.

share|improve this answer
add comment

You will need to add a CNAME entry to your DNS zone. However you might need to adjust some login in your code if its dependent on requested host, cause your scripts will not see sub1.mysite.com but sub1.com, so adjust them accordingly to handle both cases.

share|improve this answer
add comment

Sounds like you want a "Frame Redirect":

http://en.wikipedia.org/wiki/URL_redirection#Frame_redirects

share|improve this answer
    
This won't work for embedded images/css/js.. –  aularon Sep 7 '10 at 0:56
    
Ugh, there are soooooo many better ways of doing this –  Mark Henderson Sep 7 '10 at 4:53
    
Fast, good, cheap. Pick two. –  Rob Olmos Sep 7 '10 at 5:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.