当前位置:首页 > 行业动态 > 正文

httpd.ini多域名重定向

在httpd.ini中配置多域名重定向需添加对应ServerName条目,设置DocumentRoot路径,并通过Redirect或Rewrite规则实现域名跳转,保存后重启服务

httpd.ini多域名重定向配置指南

httpd.ini 文件作用

httpd.ini 是服务器(如宝塔面板、ARP 等)中用于定义虚拟主机和域名相关配置的核心文件,支持多域名绑定、重定向规则、HTTPS 强制跳转等功能。


多域名重定向核心配置

基础虚拟主机配置

每个域名需在 [virtual] 节点下单独配置,格式如下:

[virtual]
; 域名1配置
domain=example.com
root=/www/wwwroot/example.com
index=index.html index.php
; 域名2配置
domain=test.com
root=/www/wwwroot/test.com
index=index.html

强制 HTTP 转 HTTPS

若需将 HTTP 访问自动跳转至 HTTPS,添加 force-https 参数:

[virtual]
domain=example.com
root=/www/wwwroot/example.com
force-https=1

域名重定向规则

通过 serverAliasrewrite 规则实现:

  • 场景1:多域名指向同一站点

    [virtual]
    domain=main.com
    serverAlias=.main.com www.main.com
    root=/www/wwwroot/main.com

    此配置将 www.main.com 和所有子域名(如 abc.main.com)均指向同一站点。

  • 场景2:旧域名跳转至新域名

    [virtual]
    domain=old-site.com
    rewrite=^(.)$ https://new-site.com$1 permanent

    此规则将旧域名所有请求永久重定向至新域名。


常见多域名配置场景

场景需求 配置示例 说明
多域名绑定不同目录 ini<br>[virtual]<br>domain=site1.com<br>root=/path/to/site1<br>[virtual]<br>domain=site2.com<br>root=/path/to/site2 每个域名独立指向不同网站目录
HTTP 自动转 HTTPS ini<br>[virtual]<br>domain=example.com<br>force-https=1 强制所有 HTTP 请求跳转至 HTTPS
不带 www 的域名跳转至带 www ini<br>[virtual]<br>domain=example.com<br>rewrite=^(.)$ https://www.example.com$1 permanent 将裸域重定向至 www 前缀域名

注意事项

  1. 配置顺序[virtual] 节点需按优先级排列,精确匹配域名应高于通配符配置。
  2. 正则表达式rewrite 规则需符合 Nginx/Apache 语法,permanent 表示 301 重定向。
  3. 测试验证:修改后通过 nginx -t 或重启面板服务验证配置有效性。

相关问题与解答

问题1:如何配置多个子域名指向同一目录?
解答:使用通配符域名 + serverAlias

[virtual]
domain=.sub.com
root=/www/wwwroot/sub.com
serverAlias=.sub.com

此配置将所有 .sub.com 子域名(如 a.sub.com, b.sub.com)指向同一目录。


问题2:如何实现带端口的域名重定向(如 HTTP→HTTPS 不同端口)?
解答:在 rewrite 规则中指定端口号,

[virtual]
domain=example.com
rewrite=^(.)$ https://www.example.com:443$1 permanent

此规则将 http://example.com:80 请求跳转至 `https://www.example.com:443

0