How to implement "Maintenance Mode" on already established website
Asked Answered
P

7

15

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 admin to temporarily disable the website and point it to a Maintenance Mode page. This would only allow those logged in as an admin to view the website.

The options I see are:

  1. Add a new "include" file to the top of every single PHP page.

  2. I have one include that is used to display the navigation bar on
    every page (navigation class). I could write the Maintenance Mode
    code in this class.

Do I have any other options? The 1st option doesn't seem like the most efficient, and the 2nd one just seems like bad programming. Is there any other better way to include a new file on every single php file?

Thanks!

ps - the site is not launched yet

Pond answered 8/9, 2009 at 23:19 Comment(0)
K
20

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

Kathe answered 8/9, 2009 at 23:26 Comment(7)
As you can see in the examples you can exclude some IP.Kathe
That looks like a good quick alternative. I'm hoping to find other methods that may be more scalable so that each time I have a new admin, I don't have to update the .htaccess file and enter in their IP. Perhaps I'm getting a bit to ahead of myself though? For the next year I probably won't have very many new admin's. Perhaps implementing a simple quick .htaccess fix for now would work until I really need to implement the robust method. What do you guys think? Is it better to build it the "proper" way right now? Or just do the easy way until I need to make it more complex?Pond
Out of all of the options, I would say this is the best direction. It gives you the most flexibility and control site wide.Naturopathy
Well this sounds like the best solution for the time being until I really need to build something complex considering this will take me only a few minutes to put together.Pond
By the way, if anyone else uses this method, you will need to add a line into the .htaccess file to allow any external css files. To allow all css files use this: %{REQUEST_URI} !.css$Pond
@justinnl Can you describe a bit more how to insert (as a require condition?) %{REQUEST_URI} !.css$ ? Are you using the 1st link or the 2nd link in the answer?Dorsiventral
To make it work for PHP you need to add this code: RewriteCond %{REQUEST_URI} !.php$Mature
P
8

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.

Positively answered 10/6, 2013 at 20:3 Comment(3)
Well, it won't work, your header lacks an apostrophe. Else, i thought about same idea but htaccess looks far better for big sitesTopmost
Remember to clean all bytecodes related to config.php ma.ttias.be/how-to-clear-php-opcacheEdmundedmunda
why not just place header('..') in the config file?Sinfonia
D
3

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.
Distortion answered 8/9, 2009 at 23:35 Comment(4)
And you could add exit; somewhere at the end to stop the rest of your code from being parsed.Typescript
@alex: yes. should not assume people know about exit just because they got 60+ pages of php. :IDistortion
haha yes thanks. It's easy to create lots of files and not really know what you're doing still :PPond
Thanks OIS for sharing about the auto_prepend_file. I didn't know about that and I'm confident that it will come in useful later. :)Pond
K
2

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.

Kyliekylila answered 2/12, 2013 at 21:50 Comment(0)
W
1

If you're visiting this page in the 2020s, know that this feature is now built-in into the phpBB ACP (3.2 or later):

Go to ACP > General > Board configuration

And find the line that says:

Disable board:

This will make the board unavailable to users who are neither administrators nor moderators. You can also enter a short (255 character) message to display if you wish.

There ought to be a radio button there (On/Off) and a text input box where you can put the message mentioned above.

The board will remain open for logins from admins and moderators, just everybody else will be excluded (this is also great as an 'emergency switch' in case of a sudden 'bot attack to overwhelm the phpBB server!).

Finally, unlike other solutions mentioned above — which will work only under Apache — this solution is implemented at the forum level itself and does not require extensions, plugins, extra files, redirects, or special rewrites. It's not that you cannot add all of those, of course; it's just that for those that require an easy way to do an urgent maintenance now only need to click on a radio button to do so.

Wirer answered 15/7, 2022 at 21:26 Comment(0)
S
0

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).

Squilgee answered 8/9, 2009 at 23:35 Comment(0)
K
0

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>
Kobylak answered 25/4, 2015 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.