【问题标题】:PHP Implement a Down for Maintenance PagePHP实现维护页面
【发布时间】:2015-04-24 14:21:29
【问题描述】:

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

我的网站结构如下所示:

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=$1 [PT,L]
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

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

提前致谢

【问题讨论】:

  • “不起作用”到底是什么意思?您向我们展示的是哪个 .htaccess 文件?您的 webroot 文件夹是哪个文件夹?
  • 究竟是什么不工作?维护结束后重定向某人的一种快速而简单的方法是制作一些不时检查某个文件的JS。
  • 你是如何测试它的?您必须注释掉第一个条件才能对其进行测试。

标签: php .htaccess maintenance


【解决方案1】:

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

$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');
}

【讨论】:

  • 我决定使用 php 而不是 htaccess,因为它更容易。谢谢你的帮助。所有的cmets都很好,但我只能选择一个。谢谢大家。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    我来这里是因为php 标签。 ¿您是否考虑过仅通过php 进行操作?

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

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

    【讨论】:

      【解决方案4】:
      RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
      RewriteRule ^(.*)$ maintenance.html [R=302,L]
      

      将文件名更改为您想要的任何名称,您可以通过将文件重命名为该名称或从该名称重命名来打开和关闭站点,例如maintenance.html 让网站离线,maintenance.html.bak 让网站重新上线。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-26
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        • 2012-08-01
        • 1970-01-01
        • 2013-02-03
        • 2018-07-05
        相关资源
        最近更新 更多