与在Mac上使用Apache虚拟主机通配符域 [英] Wildcard domains with virtualhost with Apache on Mac

查看:176
本文介绍了与在Mac上使用Apache虚拟主机通配符域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在运行的几个领域为地方发展

I'm currently running several domains for local development

http://wordpress.dev
http://phpmyadmin.dev
http://projectx.dev
http://projecty.dev
...

大多数项目都位于用户的网站目录,但有些是位于其它地方:

Most of these projects are located in the users "Sites" directory but some are located somewhere else:

/Users/[username]/Sites/wordpress
/Users/[username]/Sites/phpmyadmin
/Users/[username]/Sites/projectx
/Users/[username]/OtherDirectory/projecty

我目前的设置都通过增加专门的条目为 / etc / hosts中的/ etc / apache2的/额外/ httpd的的虚拟主机。 CONF

/ etc / hosts文件:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0     localhost
# Virtuelle Hosts
127.0.0.1       wordpress.dev
127.0.0.1       phpmyadmin.dev
127.0.0.1       projectx.dev
....

/etc/apache2/extra/httpd-vhosts.conf:

<VirtualHost *:80>
    ServerName [PROJECT].dev
    ServerAlias [PROJECT].dev
    DocumentRoot /Users/[username]/Sites/[PROJECT]/
    <Directory /Users/[username]/Sites/[PROJECT]/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
<VirtualHost *:80>
 ...

有没有使用某种通配符设置映射像

Is there a way to use some kind of wildcard settings to map the domain to a certain directory like

http://foo.dev =>  /Users/[username]/Sites/foo
http://bar.dev =>  /Users/[username]/Sites/bar
...

而保持 projecty 工作(当然是有一些额外的设置),所以我只需要创建文件夹中的网站这是通过 HTTP即时访问:// [文件夹] .dev

while keep projecty working (with some extra settings of course) so I only have to create a folder in Sites which is instantly accessible via http://[foldername].dev

[用户名] 是很难codeD

推荐答案

使用 mod_vhost_alias 及的 VirtualDocumentRoot

只要你没有赶上虚拟主机 foo.dev 服务器名称 ServerAlias​​ 你应该能够做到以下几点:

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>

您需要的用户名是硬codeD,虽然你说这是不是一个问题。

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

% - 2 VirtualDocumentRoot 重新presents倒数第二点分隔的一部分 foo.com ,即。然后,您可以有目录,你希望映射到该网站:

%-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

您将需要确保你想以这种方式来映射任何其他域在你的hosts文件中相应的条目,或DNS如果你使用的。

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.

这篇关于与在Mac上使用Apache虚拟主机通配符域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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