如何配置多级动态生成的子域

时间:2014-09-22 21:11:02

标签: apache virtual subdomain hosts subdirectory

我想知道如何解决这类问题。首先,我的目录树看起来像:

/var/www/sites/mydomain.com/
/var/www/sites/mydomain.com/public_html/
/var/www/sites/mydomain.com/subdomains/
/var/www/sites/mydomain.com/subdomains/user01/
/var/www/sites/mydomain.com/subdomains/user01/public_html/
/var/www/sites/mydomain.com/subdomains/user01/subdomains/
/var/www/sites/mydomain.com/subdomains/user01/subdomains/a/
/var/www/sites/mydomain.com/subdomains/user01/subdomains/b/
/var/www/sites/mydomain.com/subdomains/user01/subdomains/c/
/var/www/sites/mydomain.com/subdomains/user02/
/var/www/sites/mydomain.com/subdomains/user02/public_html/
/var/www/sites/mydomain.com/subdomains/user02/subdomains/
/var/www/sites/mydomain.com/subdomains/user02/subdomains/d/
/var/www/sites/mydomain.com/subdomains/user02/subdomains/e/
/var/www/sites/mydomain.com/subdomains/user02/subdomains/f/
/var/www/sites/mydomain.com/subdomains/user03/
/var/www/sites/mydomain.com/subdomains/user03/public_html/
/var/www/sites/mydomain.com/subdomains/user03/subdomains/
/var/www/sites/mydomain.com/subdomains/user03/subdomains/g/
/var/www/sites/mydomain.com/subdomains/user03/subdomains/h/
/var/www/sites/mydomain.com/subdomains/user03/subdomains/i/

我想问你如何配置我的apache虚拟主机以将子域更改为如上所述的子目录树动态检查目录是否存在以及将子域更改为subdir:

http://(www\.)?mydomain.com /var/www/sites/mydomain.com/public_html/
http://(www\.)?user01.mydomain.com /var/www/sites/mydomain.com/subdomains/user01/public_html/
http://(www\.)?a.user01.mydomain.com /var/www/sites/mydomain.com/subdomains/user01/subdomains/a/public_html/
etc...

我试图使用VirtualDocumentRoot。 我用了

ServerAlias *.mydomain.com 

而不是

VirtualDocumentRoot /var/www/sites/mydomain.com/subdomains/%-3/public_html/ 

和一个水平的子域工作得很好,但我不知道如何用第二,第三...... n级做到这一点? 配置

ServerAlias *.*.mydomain.com 

VirtualDocumentRoot like /var/www/sites/mydomain.com/subdomains/%-3/subdomains/%-4/public_html/ 

没有给出预期的结果。 我几乎可以肯定我可以用mod_rewrite做到这一点,但不知道怎么做? 我正在尝试很多配置,但可能我的知识还不够。

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.mydomain.com\\(.*)?$
RewriteCond %{REQUEST_URI} !subdomains/(.*)?
RewriteRule (.*) /var/www/sites/mydomain.com/subdomains/%1/public_html/%2 [L]

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

我的解决方案

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{REQUEST_URI} !/subdomains/(.*)?
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.([a-z0-9]+)\.([a-z0-9]+)\.mydomain.com(/(.*)?)?
RewriteRule ^(.*)$ /var/www/sites/mydomain.com/subdomains/%3/subdomains/%2/subdomains/%1/public_html/$1 [L]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.([a-z0-9]+)\.mydomain.com(/(.*)?)?
RewriteRule ^(.*)$ /var/www/sites/mydomain.com/subdomains/%2/subdomains/%1/public_html/$1 [L]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.mydomain.com(/(.*)?)?
RewriteRule ^(.*)$ /var/www/sites/mydomain.com/subdomains/%1/public_html/$1 [L]