我想设置我的本地开发机器,以便任何对*的请求。Local被重定向到localhost。这个想法是,当我开发多个站点时,我只需向Apache添加名为site1的vhosts。地方,site2。让它们都解析到localhost,而Apache则相应地服务于不同的站点。

我用的是Windows XP。

我试着加上

127.0.0.1       *.local

到我的c:\windows\system32\drivers\etc\hosts文件,也试过:

127.0.0.1       .local

这两种方法似乎都不管用。

我知道我可以在不同的端口号上设置它们,但这是一种痛苦,因为很难记住哪个端口是哪个端口。

我不想设置本地DNS服务器,有什么建议吗?


当前回答

下面是那些试图实现目标的人的总配置(开发环境中的通配符,例如XAMPP——这个例子假设所有站点都指向相同的代码库)

Hosts文件(添加一个条目)

文件:% SystemRoot % \ system32 etc, etc drivers \ \ hosts

127.0.0.1   example.local

Httpd.conf配置(启用vhosts)

文件:etc, etc XAMPP \ \ httpd上。

# Virtual hosts
Include etc\extra\httpd-vhosts.conf

httpd-vhosts.conf配置

额外文件:XAMPP \ etc \ \ httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin admin@example.local
    DocumentRoot "\path_to_XAMPP\htdocs"
    ServerName example.local
    ServerAlias *.example.local
#    SetEnv APP_ENVIRONMENT development
#    ErrorLog "logs\example.local-error_log"
#    CustomLog "logs\example.local-access_log" common
</VirtualHost>

重新启动apache

创建pac文件:

保存成任何形式。Pac你想在任何地方,然后加载文件在浏览器的网络>代理>auto_configuration设置(重新加载,如果你改变这个)

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*example.local")) {
    return "PROXY example.local";
  }
  return "DIRECT";
}

其他回答

当您从windows命令提示符运行“ipconfig /flushdns”而不是重新启动计算机时,编辑hosts文件就不那么痛苦了。

下面是那些试图实现目标的人的总配置(开发环境中的通配符,例如XAMPP——这个例子假设所有站点都指向相同的代码库)

Hosts文件(添加一个条目)

文件:% SystemRoot % \ system32 etc, etc drivers \ \ hosts

127.0.0.1   example.local

Httpd.conf配置(启用vhosts)

文件:etc, etc XAMPP \ \ httpd上。

# Virtual hosts
Include etc\extra\httpd-vhosts.conf

httpd-vhosts.conf配置

额外文件:XAMPP \ etc \ \ httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin admin@example.local
    DocumentRoot "\path_to_XAMPP\htdocs"
    ServerName example.local
    ServerAlias *.example.local
#    SetEnv APP_ENVIRONMENT development
#    ErrorLog "logs\example.local-error_log"
#    CustomLog "logs\example.local-access_log" common
</VirtualHost>

重新启动apache

创建pac文件:

保存成任何形式。Pac你想在任何地方,然后加载文件在浏览器的网络>代理>auto_configuration设置(重新加载,如果你改变这个)

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*example.local")) {
    return "PROXY example.local";
  }
  return "DIRECT";
}

我们在本地DNS服务器中使用通配符DNS:添加一个A记录,比如*。Local -> 127.0.0.1

我认为你的网络设置需要在网络上机器的域名后缀搜索列表中有所选择的域名后缀,所以你可能想用你公司的内部域名(例如.int)取代.local,然后添加一个子域名,如.localhost.int,以明确它的用途。

因此,*.localhost.int将为网络上的每个人解析为127.0.0.1,如果端点挂起子域(例如site1.localhost.int, site2.localhost.int),则所有开发人员的配置文件设置将“正常工作”。

dnsmasq看起来也不错,但我还没有尝试过: http://ihaveabackup.net/2012/06/28/using-wildcards-in-the-hosts-file/

要回答您的问题,您不能在Windows下的hosts文件中使用通配符。

但是,如果您只想更改hosts文件以使新站点工作....你可以这样配置你的Apache,你不需要一直编辑它的配置:

http://postpostmodern.com/instructional/a-smarter-mamp/

基本上是基于我的设置的一个快速总结,在apache.conf文件中添加以下内容:

 LoadModule vhost_alias_module modules/mod_vhost_alias.so

 NameVirtualHost *:80

  <Directory "/xampp/sites">
      Options Indexes FollowSymLinks Includes ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all 
  </Directory>

  <VirtualHost *:80>
      VirtualDocumentRoot c:/xampp/sites/%-1/%-2+/
  </VirtualHost>

这允许我添加这样的条目:

127.0.0.1       test.dev

然后创建目录c:\xampp\sites\dev\test,并将必要的文件放在那里,它就可以工作了。

另一种选择是在apache.conf中使用<Directory>标记,并引用http://localhost/project/中的页面。

虽然你不能添加这样的通配符,但你可以添加你需要的完整网站列表,至少对于测试来说,这对我来说足够好,在你的hosts文件中,你只需要添加:

127.0.0.1 site1.local 127.0.0.1 site2.local 127.0.0.1 site3.local ...