You need to enable JavaScript to run this app. 如何配置Apache重写规则使别名访问时跳转至正式服务器域名
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Apache重写规则使别名访问时跳转至正式服务器域名

别名访问自动跳转正式域名的Vhost配置方案

核心修改思路

在虚拟主机的重写规则中前置增加Host头判断逻辑:

  • 当请求的Host值匹配正式ServerName(example.domain.com)时,按照原有站点配置正常处理请求
  • 当请求的Host值匹配ServerAlias定义的短别名(example)时,直接重定向到正式域名对应的同路径地址,原有页面跳转逻辑会在重定向后自动生效,达成预期访问效果

调整后的完整Vhost配置

NameVirtualHost *:8888
<VirtualHost *:8888>
  ServerName example.domain.com
  ServerAlias example
  DocumentRoot /opt/weblogic/product/virtualhosts/example.domain.com/htdocs
  ErrorLog /opt/weblogic/product/virtualhosts/example.domain.com/logs/error_log

  RewriteEngine On
  # 新增:识别别名发起的访问并跳转正式域名
  RewriteCond %{HTTP_HOST} !^example\.domain\.com$ [NC]
  RewriteRule ^/(.*)$ http://example.domain.com/$1 [R=301,L]

  # 保留原有根路径自动跳转StartPage的配置
  #RewriteRule  ^/?$ %{REQUEST_URI}/StartPage [R,L]
  RewriteOptions Inherit
  DirectoryIndex index.html startpage.jsp
  <Directory />
    Require all granted
  </Directory>
</VirtualHost>

规则生效说明

  • 新增的RewriteCond通过[NC]参数实现Host值不区分大小写匹配,避免Host大小写差异导致规则失效
  • 重定向配置采用R=301永久重定向标识,配合L参数保证规则命中后立即终止后续重写流程,不会出现循环跳转问题
  • 规则中通过$1捕获用户原始请求的完整路径,跳转后不会丢失用户原本要访问的资源位置;当用户通过别名访问根路径时,会先跳转到正式域名的根路径,再触发原有根路径跳转/StartPage的逻辑,最终访问路径会落地到http://example.domain.com/StartPage/startpage.jsp,完全匹配预期需求
  • 配置调整完成后重启Apache服务即可生效

注意:如果你的服务需要通过8888端口直接对外提供访问,只需要把重定向规则里的目标地址补上对应端口(即修改为http://example.domain.com:8888/$1)即可,规则整体逻辑不受影响。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.06 12:54:03