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

如何在Apache中配置带有动态计时的维护页面

配置带动态计时的Apache维护页面

要实现带动态计时的维护页面,需要结合服务器端脚本(比如PHP)和Apache的重定向规则,以下是具体步骤:

1. 确保Apache支持PHP模块

首先确认Apache已启用mod_php模块,以PHP 8.2为例,执行以下命令启用并重启Apache:

a2enmod php8.2
systemctl restart apache2

(根据你的PHP版本调整命令中的版本号)

2. 创建动态维护页面

在网站根目录下创建maintenance.php文件,写入以下代码(可根据需求调整样式和恢复时间):

<?php
// 设置时区为GMT
date_default_timezone_set('GMT');
// 定义服务器恢复时间,这里设置为当天GMT下午2点,可修改为具体日期如'2024-10-05 14:00:00'
$recovery_time = strtotime('today 14:00:00');
$current_time = time();
$time_left = $recovery_time - $current_time;

// 计算剩余时间的时分秒
$hours = floor($time_left / 3600);
$minutes = floor(($time_left % 3600) / 60);
$seconds = $time_left % 60;
?>
<!DOCTYPE html>
<html>
<head>
    <title>服务器维护中</title>
    <style>
        body { text-align: center; margin-top: 50px; font-family: Arial, sans-serif; }
        .countdown { font-size: 2em; color: #dc3545; margin: 20px 0; }
    </style>
</head>
<body>
    <h1>服务器维护中</h1>
    <p>服务器将于GMT时间下午2点恢复</p>
    <?php if ($time_left > 0): ?>
    <div class="countdown">剩余时间:<?php echo $hours; ?>小时<?php echo $minutes; ?>分钟<?php echo $seconds; ?>秒</div>
    <?php else: ?>
    <div class="countdown">服务器即将恢复,请稍候...</div>
    <?php endif; ?>
</body>
</html>

3. 配置Apache重定向规则

在网站根目录的.htaccess文件中添加以下规则(若没有.htaccess则新建):

# 启用重写引擎
RewriteEngine On

# 仅当维护页面存在时,将所有请求重定向到维护页面(排除维护页面本身)
RewriteCond %{REQUEST_URI} !/maintenance.php$
RewriteCond %{DOCUMENT_ROOT}/maintenance.php -f
RewriteRule ^(.*)$ /maintenance.php [R=503,L]

# 设置Retry-After响应头,告知浏览器1小时后重试(可根据实际恢复时长调整数值)
Header always set Retry-After "3600"

注意:需确保虚拟主机配置中设置了AllowOverride All,否则.htaccess规则不会生效。

关键说明

  • 使用503 Service Unavailable状态码是符合HTTP规范的维护状态,搜索引擎会识别此状态,不会影响网站的SEO表现。
  • 若不需要倒计时,仅需显示固定恢复时间,可简化PHP代码,直接输出指定时间即可。
  • 关闭维护模式时,只需删除或重命名maintenance.php文件,Apache会自动恢复正常请求处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 19:38:35