PHP 实施停机维护页面

PHP Implement a Down for Maintenance Page

我想为我的网站实施停机维护功能,但似乎遇到了一些麻烦。让我解释一下。

我的网站结构如下所示:

websitename
     model
     view
     controller
     public
          css
          .htaccess
          index.php
     .htaccess
     index.php
     maintenance.php

我正在使用此 htaccess 代码将用户重定向到维护页面。 .htaccess 文件代码位于 public 文件夹中。

<IfModule mod_rewrite.c>
    RewriteEngine On

    #handle the redirect to maintenance
    RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111
    RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
    RewriteRule .* /maintenance.php [R=302,L]


    # handle home page
    RewriteRule ^/?$ home [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Rewrite all other URLs to index.php/URL
    # RewriteRule ^(.*)$ index.php?url= [PT,L]
    RewriteRule ^(.*)$ index.php?url= [QSA,L]
</IfModule>

不过这似乎不起作用。我找了很多教程,但 none 似乎有效。另外,我想知道,用户是否被重定向到维护页面?维护结束后如何跳转到正常网站

提前致谢

忽略.htaccess文件,将代码直接移入index.php

if(file_exists('maintenance.flag')){
    // code to render maintenance page
    exit;
}

您可以通过在您的根目录中创建一个空文件 maintenance.flag 来 enable/disable 维护模式。

我是因为php标签才来到这里的。 ¿您是否考虑过仅通过 php 来完成?

我要做的是在我的 php 代码中有一个配置文件(或数据库)和一个 if 句子,例如:

if($config['maintenance']==true) {
    include_once "maintenance.php";
} else {
    // the rest of your index.php
}

如果是我,我会用您的配置值创建一个配置目录。其中之一是

$config['maintenance'] = false;
$config['maintenance_url'] = '/maintenance/';

当您想进入维护模式时,您只需更新配置文件维护值

$config['maintenance'] = true;

并在您的前端控制器或索引文件中进行检查

if($config['maintenance'])
{
    header('Location: ' . $config['maintenance_url'] );
    // in the event page cannot redirect
    die('Under maintenance, please come back later');
}
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteRule ^(.*)$ maintenance.html [R=302,L]

将文件名更改为您想要的任何名称,您可以通过将文件重命名为该名称或从该名称重新命名来打开和关闭站点,例如maintenance.html 使站点脱机,maintenance.html.bak 使其恢复联机。