我正在字符串中设置两个虚拟主机(example.com.conf和test.com.conf):
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
和
<VirtualHost *:80>
ServerAdmin admin@test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
问题是,如果我在浏览器中访问localhost,它会显示在网站www.example.com上。如果我键入localhost/test.com,则找不到错误。我的目标应该是用localhost/example.com和localhost/test.com.来处理这两个网站
听起来你可以使用apache的ServerPath指令,在你的情况下,这样的东西应该可以工作:
<VirtualHost 127.0.0.1>
# primary vhost
DocumentRoot "/var/www/example.com"
RewriteEngine On
RewriteRule "." "/var/www/example.com/public_html"
# ...
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "/var/www/example.com/public_html"
ServerName localhost
ServerPath "/example/"
RewriteEngine On
RewriteRule "^(/sub1/.*)" "/var/www/example$1"
# ...
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "/var/www/test.com/public_html"
ServerName localhost
ServerPath "/test/"
RewriteEngine On
RewriteRule "^(/sub2/.*)" "/var/www/test$1"
# ...
</VirtualHost>
第一个Vhost将使localhost默认为example.com页面。
如果您想浏览到任何虚拟主机下的这些文件夹,如http://localhost/test.com,那么您只需要在任何活动虚拟主机的末尾附加的位置标签中添加一个alias
指令
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster@localhost
DocumentRoot /var/www
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Location "/example.com">
Alias "/var/www/example.com/public_html"
</Location>
<Location "/test.com">
Alias "/var/www/test.com/public_html"
</Location>
或者,如果你不想在其他地方使用,你可以把它放在虚拟主机里
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster@localhost
DocumentRoot /var/www
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Location "/example.com">
Alias "/var/www/example.com/public_html"
</Location>
<Location "/test.com">
Alias "/var/www/test.com/public_html"
</Location>
</VirtualHost>