Wildcard domains with virtualhost with Apache on Mac

做~自己de王妃 提交于 2019-12-02 00:22:19

Use mod_vhost_alias and a VirtualDocumentRoot

As long as you have no VirtualHost catching foo.dev as a ServerName or ServerAlias you should be able to do the following:

<VirtualHost *:80>
  ServerName default.dev

  VirtualDocumentRoot /Users/youruser/%-2
  ...
  ...
</VirtualHost>

You'll need the username to be hardcoded, though you say that's not a problem.

%-2 in the VirtualDocumentRoot represents the penultimate dot-separated part of foo.com, i.e. foo. You could then have directories as you wish that map to the sites:

http://foo.dev =>  /Users/youruser/Sites/foo
http://bar.dev =>  /Users/youruser/Sites/bar
http://subdom.baz.dev =>  /Users/youruser/Sites/baz

You'll need to make sure that any additional domains you want to map in this fashion have an appropriate entry in your hosts file, or DNS if you're using that.

In order to accomplish what you need, you can configure a dynamically configured mass virtual host.

To have it you should add something like the following to your httpd.conf file:

# get the server name from the Host: header
UseCanonicalName Off

# this log format can be split per-virtual-host based on the first field
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon

# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /www/hosts/%0/docs
VirtualScriptAlias /www/hosts/%0/cgi-bin 

That would do the same as the following:

NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
    ServerName www.customer-1.com
    DocumentRoot /www/hosts/www.customer-1.com/docs
    ScriptAlias /cgi-bin/ /www/hosts/www.customer-1.com/cgi-bin
</VirtualHost>
<VirtualHost 111.22.33.44>
    ServerName www.customer-2.com
    DocumentRoot /www/hosts/www.customer-2.com/docs
    ScriptAlias /cgi-bin/ /www/hosts/www.customer-2.com/cgi-bin
</VirtualHost>
# blah blah blah
<VirtualHost 111.22.33.44>
    ServerName www.customer-N.com
    DocumentRoot /www/hosts/www.customer-N.com/docs
    ScriptAlias /cgi-bin/ /www/hosts/www.customer-N.com/cgi-bin
</VirtualHost> 

For more details on this, check here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!