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

如何通过Apache配置实现域名映射到另一域名子目录且不修改地址栏

现有配置的核心问题

  1. 反向代理依赖的mod_proxy、mod_proxy_http模块未启用,[P]代理规则无法正常执行
  2. 重写规则的路径拼接存在冗余斜杠问题,根路径请求会被转为domain1.example/path//,匹配失败
  3. 若两个域名部署在同一台Apache实例上,不需要走反向代理,直接修改站点根目录配置即可,性能更优

解决方案

场景1:两个域名均部署在当前EC2实例(推荐)

无需走反向代理,直接调整domain2.example的站点根目录即可满足路径映射要求,且地址栏不会发生变化。

  1. 删除项目目录下之前添加的.htaccess重写规则,避免配置冲突
  2. 修改/etc/apache2/sites-available/000-default.conf中domain2.example的VirtualHost配置:
<VirtualHost *:80>
        ServerName "domain2.example"
        ServerAdmin email@domain1.example
        DocumentRoot /var/www/html/path
        ErrorLog ${APACHE_LOG_DIR}/domain2-error.log
        CustomLog ${APACHE_LOG_DIR}/domain2-access.log combined
        <Directory /var/www/html/path>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>
</VirtualHost>

如果配置了HTTPS,在443端口的VirtualHost中添加同样的DocumentRoot规则即可。
3. 检查配置语法并重启Apache生效:

apache2ctl configtest
systemctl restart apache2

场景2:domain1.example部署在其他服务器,必须走反向代理

  1. 先启用必要的Apache模块:
a2enmod proxy proxy_http rewrite
systemctl restart apache2
  1. 修改domain2.example的VirtualHost配置:
<VirtualHost *:80>
        ServerName "domain2.example"
        ServerAdmin email@domain1.example
        ErrorLog ${APACHE_LOG_DIR}/domain2-error.log
        CustomLog ${APACHE_LOG_DIR}/domain2-access.log combined
        ProxyRequests Off
        ProxyPreserveHost On
        RewriteEngine On
        RewriteRule ^(.*)$ https://domain1.example/path$1 [P,L]
        <Directory /var/www/html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>
</VirtualHost>
  1. 检查配置语法后重启Apache生效。

内容的提问来源于stack exchange,提问作者pratv

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 02:15:04