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.

So I use following schema to handle multiple sites on my apache:

<VirtualHost 192.168.1.100:80>

# get the server name from the Host: header
UseCanonicalName Off

VirtualDocumentRoot /var/www/%0/docs
VirtualScriptAlias /var/www/%0/cgi-bin

</VirtualHost>

Therefore, if a client go to www.example.com, it will actually point to /var/www/www.example.com/doc/, which is good. However, what if the client go to example.com? It will point to /var/www/example.com/doc, which is not what we want.

So my question is: is there any better schema for that? Or what should I do to fix the issue?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

This depends what behavior you want to see.

If you want for the www.example.com site to be redirected to example.com or vice-versa, this can be implemented in RewriteRule settings. For example (these go inside your <VirtualHost>):

# Send www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^/(.*)$ http://example.com/$1 [R=301,L]
# Send www.other.example.com to other.example.com
RewriteCond %{HTTP_HOST} ^www\.other\.example\.com$ [NC]
RewriteRule ^/(.*)$ http://other.example.com/$1 [R=301,L]

To knock the www off of every name:

RewriteCond ${HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]

If you want to serve the content to both names, then you have a couple options.

  • If you'll only ever have a pretty simple set of domains, then change the constructed docroot to not care about www.

    VirtualDocumentRoot /var/www/%-2/docs
    

    ...which points to the docroot at /var/www/example/docs.

  • If you need to get a little more creative with the subdomains (like having a b.example.com and www.b.example.com that point to their own site, not the example.com one), symlinks might be a good approach.

    Options +FollowSymLinks
    

    ...and create a symlink at /var/www/www.example.com pointing to the directory at /var/www/example.com.

share|improve this answer
    
Could you please advise how to use RewriteRule you mentioned above? An example will be much helpful, thanks! –  cache Jun 27 '12 at 22:27
    
@cache Added an example for the redirect option. –  Shane Madden Jun 27 '12 at 22:30
    
Thanks, what should I change to make it generic rather than only applied for www.example.com? –  cache Jun 27 '12 at 22:48
    
@cache Edited that in too. –  Shane Madden Jun 27 '12 at 23:04
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.