How to implement “Maintenance Mode” on already established website

后端 未结 6 928
天命终不由人
天命终不由人 2020-12-04 20:33

I have built a website (PHP) with more than 60 pages. I have only now realized (unfortunately) that I should have built in an \"In Maintenance Mode\" feature to allow an adm

相关标签:
6条回答
  • 2020-12-04 20:52

    You can use .htaccess to redirect to another page while on Maintenance Mode.

    Three assorted examples:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
    RewriteCond %{REQUEST_URI} !^/maintenance\.html$
    RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]
    

    .htaccess “Down For Maintenance” Page Redirect

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !/maintenance.html$ 
    RewriteCond %{REMOTE_HOST} !^888\.888\.888\.888
    
    RewriteRule $ /maintenance.html [R=302,L] 
    

    Redirect to maintenance page during upgrade using .htaccess

    # redirect all visitors to alternate site but retain full access for you
    ErrorDocument 403 http://www.alternate-site.com
    Order deny,allow
    Deny from all
    Allow from 99.88.77.66
    

    Maintenance mode for apache

    0 讨论(0)
  • 2020-12-04 21:07

    The simplest way would be to centralize some of the logic regarding site generation. This way you could turn maintenance mode on and redirect any non admins to a different page. This would not be hard to do, as I imagine you have some code that keeps popping up all over the place, so just extend the login functionality to check for a global variable (if you don't have any common global variables across your page you could just set it in .htaccess via setenv).

    0 讨论(0)
  • 2020-12-04 21:09

    I know this is an old question but I am adding this for future searchers who come across this page via searching.

    I use a PHP redirect.

    At the top of every page add the following lines:

    <?php
    include('PATH/TO/FILE/config.php');
    if(maintenance == 1) {
    header('Location: http://example.com/maintenance.php');
    }
    else {
    // do nothing
    }
    ?>
    

    And in your config.php file just make a variable like so

    $maintenance = 0; // turns maintenance mode off
    

    When you want your site to be in maintenance mode just change the "0" to a "1". This will redirect all your site visitors to a maintenance page.

    I typed that code pretty fast without testing it but it should work.

    0 讨论(0)
  • Rather than specify IP Addresses, you could also check against HTTP_USER_AGENT in a RewriteCond..

    RewriteCond %{HTTP_USER_AGENT} !MyCrazyString
    RewriteCond %{THE_REQUEST} !upgrade\.txt
    RewriteRule .* /upgrade.txt [R=307,L]
    

    Anyone with "MyCrazyString" tagged onto the end of their Browser's User Agent string can get access, everyone else gets the maintenance mode page/site.

    So long as high security isn't required, it's ideal; quick and easy to disseminate to admins, completely portable, and importantly, won't break if you reconnect your DSL when the only guy with actual server access is asleep at the other side of the world.

    ;o) Cor

    0 讨论(0)
  • 2020-12-04 21:16

    I think, standard Apache expressions are more easily to understand than rewrite rules.

    Before maintenance I rename file /home/coolcmd/site_maintenance_off to /home/coolcmd/site_maintenance_on.

    Code snippet for /.htaccess:

    # If file, directory or symlink /home/coolcmd/site_maintenance_on exists,
    # and requested file is not /maintenance.html,
    # and not an admin's IP...
    <If "-e '/home/coolcmd/site_maintenance_on' && %{REQUEST_URI} != '/maintenance.html' && %{REMOTE_ADDR} != '111.111.111.111'">
    # Generate status code 503 for URL beginning with /, in other words for ANY URL.
    # This is not "the real redirection 3xx".
    Redirect 503 "/"
    # This page handles status code 503. All external resources used by page
    # (for example, maintenance.css), if any, must be added to <if> above.
    ErrorDocument 503 /maintenance.html
    # Maintenance probably will end in 10 minutes.
    Header set Retry-After 600
    </If>
    
    0 讨论(0)
  • 2020-12-04 21:18

    auto_prepend_file string

    Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require() function, so include_path is used.

    The special value none disables auto-prepending.

    you can set this in php.ini, or in apache (virtual) host file or .htaccess with php_flag auto_prepend_file file.php

    [or php_admin_flag (?)]

    edit

    • Maybe you should not put the include file in your web root dir or a sub folder.
    • And remember to call exit or die at the end.
    0 讨论(0)
提交回复
热议问题