apache 通配符子域和静态子域的虚拟主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/758351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:30:17  来源:igfitidea点击:

Virtualhost For Wildcard Subdomain and Static Subdomain

apachesubdomainvirtualhostwildcard-subdomain

提问by Dave

I have an odd situation where I want to have the URLs app1.example.com, example.comand *.example.comall using a different virtual host. This is what I have (excluding example.combecause it just makes it messier).

我有一个奇怪的情况,我想要 URLs app1.example.comexample.com并且*.example.com都使用不同的虚拟主机。这就是我所拥有的(不包括example.com因为它只会让它变得更加混乱)。

<VirtualHost *>
  ServerName app1.example.com
  ServerAlias app1.example.com

  DocumentRoot = /var/www/app1
  # Other configuration for this app here

</VirtualHost>

<VirtualHost *>
  ServerName wildcard.example.com
  ServerAlias *.example.com

  DocumentRoot = /var/www/wildcard
  # other configuration for this app here

</VirtualHost>

The problem is that they conflict. Whichever one is listed first wins out. How can I host both a wildcard virtualhost and a specific one?

问题在于它们之间存在冲突。先上榜者胜出。如何同时托管通配符虚拟主机和特定虚拟主机?

Note: I'm not just changing DocumentRootin the config, so using mod_rewriteto change the DocumentRoot variable does not fix it.

注意:我不只是DocumentRoot在配置中mod_rewrite进行更改,因此使用更改 DocumentRoot 变量并不能修复它。

回答by Tim

<VirtualHost *:80>
  DocumentRoot /var/www/app1
  ServerName app1.example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/example
  ServerName example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/wildcard
  ServerName other.example.com
  ServerAlias *.example.com
</VirtualHost>

Should work. The first entry will become the default if you don't get an explicit match. So if you had app.otherexample.com point to it, it would be caught be app1.example.com.

应该管用。如果您没有得到明确的匹配,第一个条目将成为默认值。因此,如果您让 app.otherexample.com 指向它,它将被捕获为 app1.example.com。

回答by Christopher Shaw

Wildcards can only be used in the ServerAliasrather than the ServerName. Something which had me stumped.

通配符只能用于ServerAlias而不是ServerName. 有什么让我难倒的。

For your use case, the following should suffice

对于您的用例,以下内容就足够了

<VirtualHost *:80>
    ServerAlias *.example.com
    VirtualDocumentRoot /var/www/%1/
</VirtualHost>

There is also more information at https://www.chris-shaw.com/blog/using-wildcards-in-virtual-hosts-on-apache

https://www.chris-shaw.com/blog/using-wildcards-in-virtual-hosts-on-apache还有更多信息

回答by James Marks

This also works for https needed a solution to making project directories this was it. because chrome doesn't like non ssl anymore used free ssl. Notice: My Web Server is Wamp64 on Windows 10 so I wouldn't use this config because of variables unless your using wamp.

这也适用于 https 需要一个解决方案来制作项目目录就是这样。因为 chrome 不再喜欢非 ssl 使用的免费 ssl。注意:我的 Web 服务器是 Windows 10 上的 Wamp64,因此除非您使用 wamp,否则我不会因为变量而使用此配置。

<VirtualHost *:443>
ServerAdmin [email protected]
ServerName test.com
ServerAlias *.test.com

SSLEngine On
SSLCertificateFile "conf/key/certificatecom.crt"
SSLCertificateKeyFile "conf/key/privatecom.key"

VirtualDocumentRoot "${INSTALL_DIR}/www/subdomains/%1/"

DocumentRoot "${INSTALL_DIR}/www/subdomains"
<Directory "${INSTALL_DIR}/www/subdomains/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>