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

Apache2配置:域名重定向后保留原URL地址栏显示

解决方案:保持地址栏显示原域名的反向代理配置

问题背景

域名服务商已将fharrell.com/*重定向到hbiostat.org/blog/*,但希望用户访问时地址栏始终显示fharrell.com/*,而非跳转后的域名路径。

当前Apache配置情况

  • /etc/apache2/apache2.conf为标准配置,仅修改了目录权限部分:
<Directory /home/ubuntu/htdocs/>
        Options FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>
  • 站点配置文件/etc/apache2/sites-enabled/hbiostat.org.conf是/etc/apache2/sites-available/hbiostat.org.conf的符号链接
  • 该配置文件将文档根目录设为/home/ubuntu/htdocs,此前服务运行正常
  • 完整的hbiostat.org.conf内容如下:
<VirtualHost *:443>
    ServerAdmin my@email.address
    DocumentRoot /home/ubuntu/htdocs
    ServerName  hbiostat.org
    ServerAlias www.hbiostat.org
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /home/ubuntu/htdocs>
  Options FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>

<Directory /home/ubuntu/htdocs/blog>
RewriteEngine on
RewriteBase /
RewriteRule ^hbiostat\.org/blog$ fharrell.com [R]
</Directory>
       Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hbiostat.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hbiostat.org/privkey.pem
</VirtualHost>

<VirtualHost *:80>
ServerName hbiostat.org
ServerAlias www.hbiostat.org
DocumentRoot /home/ubuntu/htdocs
<Directory /home/ubuntu/htdocs/blog>
RewriteEngine on
RewriteBase /
RewriteRule ^hbiostat\.org/blog$ fharrell.com [R]
</Directory>
</VirtualHost>

已尝试的操作(均无效)

  • 用sudo apachectl -t验证配置语法,无错误
  • 用sudo a2enmod rewrite确认mod_rewrite模块已启用
  • 执行sudo systemctl restart apache2重启服务器
  • 尝试过多种重定向相关配置方案,也曾尝试将规则写入.htaccess(希望避免此方式),但地址栏仍显示hbiostat.org/blog/*

正确配置方案

你当前的Rewrite规则逻辑错误,因为你是在hbiostat.org的虚拟主机里尝试跳转到fharrell.com,但实际需要的是反向代理——当请求到达hbiostat.org/blog/*时,返回对应内容但让地址栏保持fharrell.com/*。具体配置步骤如下:

步骤1:启用必要的Apache模块

sudo a2enmod proxy proxy_http
sudo systemctl restart apache2

步骤2:修改hbiostat.org的虚拟主机配置

将原有的Rewrite规则替换为反向代理规则,同时把fharrell.com添加为ServerAlias:

443端口虚拟主机修改:

<VirtualHost *:443>
    ServerAdmin my@email.address
    DocumentRoot /home/ubuntu/htdocs
    ServerName  hbiostat.org
    ServerAlias www.hbiostat.org fharrell.com www.fharrell.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /home/ubuntu/htdocs>
  Options FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>

# 反向代理规则:匹配fharrell.com的请求,转发到/blog路径
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?fharrell\.com$
RewriteRule ^(.*)$ /blog/$1 [P,L]

<Directory /home/ubuntu/htdocs/blog>
  Options FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hbiostat.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hbiostat.org/privkey.pem
# 若fharrell.com有独立SSL证书,需在此添加对应证书配置
</VirtualHost>

80端口虚拟主机修改:

<VirtualHost *:80>
ServerName hbiostat.org
ServerAlias www.hbiostat.org fharrell.com www.fharrell.com
DocumentRoot /home/ubuntu/htdocs

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?fharrell\.com$
RewriteRule ^(.*)$ /blog/$1 [P,L]
</VirtualHost>

步骤3:重启Apache服务

sudo apachectl -t && sudo systemctl restart apache2

关键说明

  • [P]标记是核心:告诉Apache使用反向代理而非重定向,这样地址栏不会发生变化
  • 需确保fharrell.com的DNS解析指向当前服务器,或域名服务商的重定向为透明重定向/反向代理类型(而非普通3xx跳转)
  • 若fharrell.com有单独的SSL证书,需在443虚拟主机中补充对应的证书配置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 23:31:19