Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...