Apache 中的变量/动态/REGEX 虚拟主机?
今天只是一个奇怪的问题。是否可以根据请求的子域来改变虚拟主机的 DocumentRoot?
<VirtualHost *>
ServerName ^VARIABLE$.example.com
DocumentRoot ~/Sites/^VARIABLE$
</VirtualHost>
Just an off the wall question today. Is it posible to vary DocumentRoot of a virtualhost based on the subdomain requested like so?
<VirtualHost *>
ServerName ^VARIABLE$.example.com
DocumentRoot ~/Sites/^VARIABLE$
</VirtualHost>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的:
步骤 1:设置通配符 DNS
您必须添加指向服务器 IP 的 A 记录,如下所示:
步骤 2:设置 apache VirtualHost
请注意重要的行:
ServerAlias *.example.com< /代码>。这将告诉 Apache 任何带有 .example.com 后缀的主机也将与该虚拟主机匹配。
步骤 3:设置重写规则
您必须将此行添加到位于 Web 根文件夹中的
.htaccess
文件中(例如/home/www/www.example.com/htdocs):
这样,对
foo.example.com
的请求会将访问者重定向到example.com/foo
等等。祝你好运。(Reference: http://www.debian-administration.org/articles/358)
Yes it is possible:
Step1: Setting up Wildcard DNS
You have to add an A Record that points to your server's IP like that:
Step2: Set up apache VirtualHost
Notice the important line:
ServerAlias *.example.com
. This will tell Apache that any host with the .example.com suffix will match this virtual host too.Step3: Setting up Rewrite Rules
You have to add this lines in your
.htaccess
file located in your web root folder (eg./home/www/www.example.com/htdocs
):That way a request for
foo.example.com
will redirect visitors toexample.com/foo
and so on. Good luck.(Reference: http://www.debian-administration.org/articles/358)