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.

With Mac OS X 10.6, I'm having some fun moving from the old way of hosting multiple sites (having a block of code in a .conf file for each virtual host) to a slightly less old way (telling the server to look in /sites/ for a folder named example.com and serve whatever is in there.)

Making this happen only requires a couple things: in /etc/apache2/httpd.conf, uncomment this line

#Include /private/etc/apache2/extra/httpd-vhosts.conf

and below that add

VirtualDocumentRoot /sites/%0

Then, in

/etc/apache2/extra/httpd-vhosts.conf

add a block of code that says

<Directory /sites>
 Order Deny,Allow
 Allow from all
</Directory>

My only problem is I can't find an easy way to serve the same content if the visitor requests example.com or www.example.com. With the old way it's easy to make it respond to any subdomain. I'm sure there's a way to do this with the Apache config so I don't have to resort to making lots of symlinks in /sites/. I couldn't find an obvious answer at http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html

Ideally, not only would I like to serve the same content with or without the 'www', I'd like to be able to still use subdomains as needed with different, so I'd rather not have everything (*.example.com) serve from the same folder.

share|improve this question
1  
I don't think it's possible unless you find a place to put ServerName mydomain.comand ServerAlias www.mydomain.com. Maybe you could do it with a DNS redirect? –  Bart De Vos Apr 11 '11 at 8:14
    
You definitely should use the solution proposed by TiZon (With ServerName and ServerAlias) and then use mod_rewrite to rewrite the non-preferred domain to the preferred one with 301. Otherwise google will treat your sites and incoming links as 2 different ones with duplicate content (unless you use the "canonical url" parameter) –  shakalandy Apr 11 '11 at 8:59
add comment

1 Answer

You're going to have problems with cookies not crossing over between the sites if you have (effectively) 2 virtual hosts with the same document root. A better solution would be to redirect everything for a non www domain to the corresponding www domain. There are lots of different ways to do this, e.g. using a 404 handler in the (empty) default vhost or using mod_rewrite on %{SERVER_NAME}

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.