带有正则表达式的 apache 虚拟主机定义 [英] apache virtual host definition with regex

查看:40
本文介绍了带有正则表达式的 apache 虚拟主机定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于开发项目,我使用主机文件将真实域指向本地主机.我将虚拟主机定义添加到 apache 配置文件.我的问题是是否可以将所有xyz.com"域重定向到d:/xampp/htdocs/websites/xyz.com"目录?这样我就不需要每次都添加 vhost 定义.

For development projects I point real domains to localhost using hosts file. and I add virtual host definition to apache config file. My question is it possible to redirect all "xyz.com" domains to "d:/xampp/htdocs/websites/xyz.com" directory ? this way I will not need to add vhost definition everytime.

推荐答案

您可以在 VirtualHostServerAlias 指令中使用通配符:

You can use a wildcard in your VirtualHost's ServerAlias directive:

<VirtualHost *:80>
  # Official name is example.com
  ServerName example.com

  # Any subdomain *.example.com also goes here
  ServerAlias *.example.com

  DocumentRoot "D:/xampp/htdocs/websites/xyz.com"

  # Then rewrite subdomains into different directories
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^(.*)\.example.com$
  # Use the %1 captured from the HTTP_HOST
  # For example abc.example.com writes to websites/abc.com
  RewriteRule ^(.*)$ "D:/xampp/htdocs/websites/%1.com/$1" [L]
</VirtualHost>

这篇关于带有正则表达式的 apache 虚拟主机定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆