You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加Apache2子域名demo.example.com后无法访问的问题求助

Apache2子域名demo.example.com后无法访问的问题求助

各位好,我现在遇到了Apache虚拟主机配置的问题,折腾半天没搞定,来请教大家。

先说说我的现有环境:

  • 域名example.com已经指向运行Apache2的服务器
  • 目前配置了HTTP和HTTPS两个虚拟主机,能正常处理example.com的请求,指向/var/www/html/example目录,并且设置了强制HTTPS跳转,一切正常。

我的需求是:
添加子域名demo.example.com,让它直接指向服务器的根HTML目录/var/www/html/,这样访问demo.example.com/some-folder就能显示对应文件夹里的内容。

我尝试的操作:

  1. 新建了HTTP虚拟主机配置文件exampledemo.com.conf(后来怀疑是不是文件名应该和域名对应?比如demo.example.com.conf?不过先按我实际操作的来说),内容如下:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ServerName demo.example.com
ServerAlias www.demo.example.com
ErrorLog logs/demo.example.com-error_log
CustomLog logs/demo.example.com-access_log common
Redirect permanent / https://demo.example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =demo.example.com [OR]
RewriteCond %{SERVER_NAME} =www.demo.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
  1. 用certbot生成了对应的HTTPS虚拟主机配置,没做任何修改
  2. 启用了这个新站点,重启了Apache服务

但问题来了:
访问demo.example.com的时候,跳转到的是我服务器上的fallback页面——也就是000-default.conf里配置的/var/www/html/not-found目录的内容,说明我的新虚拟主机配置根本没匹配上请求。

下面贴出我所有相关的配置文件:

现有example.com的HTTP配置(example.com.conf)

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/example
ServerName example.com
ServerAlias www.example.com
ErrorLog logs/example.com-error_log
CustomLog logs/example.com-access_log common
Redirect permanent / https://example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

我新建的demo子域名HTTP配置(exampledemo.com.conf)

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ServerName demo.example.com
ServerAlias www.demo.example.com
ErrorLog logs/demo.example.com-error_log
CustomLog logs/demo.example.com-access_log common
Redirect permanent / https://demo.example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =demo.example.com [OR]
RewriteCond %{SERVER_NAME} =www.demo.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

默认fallback配置(000-default.conf)

<VirtualHost *:80>
DocumentRoot /var/www/html/not-found
</VirtualHost>

有没有大佬能帮我看看哪里出错了?感激不尽!

备注:内容来源于stack exchange,提问作者Johnczek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 11:27:31