虚拟DocumentRootconfiguration为WWW和非WWW – 没有redirect

我搜查了很多,但找不到答案

在Apache中设置dynamic虚拟主机时,可以这样做:

RewriteEngine On # a ServerName derived from a Host: header may be any case at all RewriteMap lowercase int:tolower ## deal with normal documents first: # allow Alias /icons/ to work - repeat for other aliases RewriteCond %{REQUEST_URI} !^/icons/ # allow CGIs to work RewriteCond %{REQUEST_URI} !^/cgi-bin/ # do the magic RewriteRule ^/(.*)$ /var/www/domains/${lowercase:%{SERVER_NAME}}/html/$1 ## and now deal with CGIs - we have to force a MIME type RewriteCond %{REQUEST_URI} ^/cgi-bin/ RewriteRule ^/(.*)$ /var/www/domains/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [T=application/x-httpd-cgi] 

问题是,你需要创build2个文件夹:example.com和www.example.com(或创build一个和它的符号链接)

什么是重写规则,可以避免必须创build这2个文件夹? (或符号链接)

这个问题也适用于NGINX

像这样的问题: 有没有“WWW”的Apache虚拟主机? 我不想使用* .example.com

添加另一个重写规则,应该在configuration文件中的另一个之前:

 RewriteCond %{SERVER_NAME} ^www\.(.*) RewriteRule ^/(.*)$ /www/hosts/${lowercase:%1}/docs/$1 

你在这里做的是将域名与正则expression式匹配。 如果域名以“www。”开头,则“www。”后面的所有内容 将被存储在variables%1中。 在下一行中,您将重新使用该variables而不是整个ServerName。