4

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.

bkilinc
  • 989
  • 2
  • 13
  • 28
  • I would like every *.com domain to go different folders under "D:/xampp/htdocs/websites/". mywebsite.com should go "D:/xampp/htdocs/websites/mywebsite.com". otherwebsite.com should go ""D:/xampp/htdocs/websites/otherwebsite.com"" – bkilinc Jun 23 '12 at 13:48
  • You can edit your question to add this updated information instead of adding a comment. – beerbajay Jun 23 '12 at 14:16
  • I found that, this can be possible with mod_vhost_alias module, but I am not experienced with it. I will investigate, and when I find the solution I will post it here, for sharing. – bkilinc Jun 24 '12 at 11:25

1 Answers1

12

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>
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • I would like every *.com domain to go different folders under "D:/xampp/htdocs/websites/". mywebsite.com should go "D:/xampp/htdocs/websites/mywebsite.com". otherwebsite.com should go ""D:/xampp/htdocs/websites/otherwebsite.com"" – bkilinc Jun 23 '12 at 13:48
  • @bkilinc Sorry I didn't have time to finish this earlier. I restored my answer along with rewrite rules that rewrite the subdomain into a directory name. – Michael Berkowski Jun 23 '12 at 19:57