基于域的静默重定向

I've set up my own server.

Let's say my main server folder is /home/www and I've set the nameservers so that all domains point to this location.

Now, here's what I want to do :

  • Have each domain's files in a separate subfolder
  • Based on the domain requested, silently redirect to the appropriate subfolder

E.g.

  • if we need somedomain.com or www.somedomain.com or www.somedomain.com/anything/ (or any such variation for that matter) redirect the request from /home/www/ to /home/www/somedomain.com/

How can this be done?


And here's what I've tried (but given that .htaccess is definitely ... not my thing, it'll most likely be close to non-sensical...) :

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www.)?somedomain.com$ [NC]
RewriteRule ^(/)?$ somedomain.com [L]

There are two possible choices. you can create Virtual Host for each domain and set virtual document root. Or You can use rewrite rule.

From apache Virtual host documentation:

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/www.example1.com
ServerName www.example1.com

# Other directives here

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/www.example2.org
ServerName www.example2.org

# Other directives here

</VirtualHost> 

You can find out more in http://httpd.apache.org/docs/2.2/vhosts/examples.html

永久性全球ROOT系统