Helo! I would like to prepare a dynamic virtual host for all subdomains that will be created in future using mod_rewrite. All subdomains would be configured prety much same way, so i thought of using dynamic VH configuration. I want my document root for each subdomain to be /home/user/public_html/subdomainName.
I've tried with following configuration, but had no success:
<VirtualHost xxx.xxx.xxx.xxx:80>
# get the server name from the Host: header
UseCanonicalName Off
<Directory /home/user/public_html/>
# ExecCGI is needed here because we can't force
# CGI execution in the way that ScriptAlias does
Options FollowSymLinks ExecCGI
</Directory>
RewriteEngine On
# a Serve开发者_如何学CrName derived from a Host: header may be any case at all
RewriteMap lowercase int:tolower
#rule that is suposed to set document root of virtual host!???
RewriteRule ^([a-z-]+)\.domain\.com/?(.*) /home/user/public_html/$1/$2
</VirtualHost>
The rule or somethinh seems to be wrong andit doesn't apply. I've never worked with dynamic VH before so i have no idea where i'm wrong...
This is a need a lot of people had before you. So there's an apache module which can do that for you mod_vhost_alias http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html
Provides for dynamically configured mass virtual hosting
instead of dynamic virtual hosts, you can also do this using wildcard dns which means configuring {anything}.yourdomain.tld to point to your server.
This will also get you same document root for each domain.
note that you will need to add ServerAlias *.yourdomain.tld
to the virtual host entry
With RewriteRule you can't access the domainname. Also use RewriteCond for that part and use %1 etc. for backreference.
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) %1/$1
The only problem is this will create a continues loop of prepending the subdomainname string. So what I normally do is create a separate folder for subdomains, like 'subdomains'
RewriteRule ^subdomains - [L]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) subdomains/%1/$1
精彩评论