【问题标题】:VirtualHosts fallback to rewriting URLVirtualHosts 回退到重写 URL
【发布时间】:2013-05-30 07:50:36
【问题描述】:

我正在设置一个 Apache Web 服务器,并且我的 httpd.conf 中有一堆 VirtualHost 条目,它们都可以正常工作。我的域(例如 test.mydomain.com、test2.mydomain.com)上的所有子域请求都发送到相同的 IP 地址(相同的服务器)。如果没有针对它们的 VirtualHost 条目,我想将任何无法识别的子域重写为简单的 www.mydomain.com 或 mydomain.com。这可以使用 Apache 实现吗?

谢谢。

【问题讨论】:

    标签: apache dns rewrite virtualhost subdomain


    【解决方案1】:

    您可以在默认的 VirtualHost 中设置重定向规则(即,通过没有 VirtualHost 的主机访问服务器时使用的规则):

    Redirect permanent / http://example.com/
    

    如果这不符合您的需求(例如,如果您希望仍然能够访问备用 DocumentRoot 上的某些文件),您可以将 PHP 脚本设置为您的 404 处理程序:

    httpd.conf

    ErrorDocument 404 /404.php
    

    404.php

    <?php
    header($_SERVER["SERVER_PROTOCOL"] . " 301 Moved Permanently");
    header("Location: http://example.com");
    

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 2016-04-18
      • 2020-06-23
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多