How to put a site into maintenance mode while still allowing only me to access?
Asked Answered
K

2

16

I am about to upgrade my whole site and I am looking for a way to do the maintenance process, but still allowing me to access.

I am thinking of using htaccess. However, that only redirects users accessing for example, index.php to maintenance.php, right? If a user accesses dashboard.php, there will be no redirection.

During the maintenance, I do need to access index.php (ONLY ME).

Kcal answered 16/5, 2012 at 16:9 Comment(6)
htaccess can redirect ALL REQUESTS to maintenance.phpFoliose
I was going to give the solution but I don't work for a start up.Micamicaela
@Kcal You should edit your question to ask how one might put a site into maintenance mode, while still reserving access for employees, using PHP/.htaccess. Focus only on the technical aspects, leaving out questions about startups, etc.Rogue
There are many ways to do this, and it depends entirely on your specific needs and architecture. For instance, do you need to access your whole site, or just one part of it during your maintenance window? Do you take your whole site down for this, or parts of it? Do you use a load balancer?Trophic
See ".htaccess mod_rewrite IP-based redirect: how to redirect all traffic to a specific subdirectory, except my IP?"Rogue
Jonathan, your solution is great, if you know all the IPs that your staff comes from. If you have staff working from their private homes, or you need to implement a staff/non-staff selector over a long period of time in which IP addresses change (my DSL connection is reset every 24 hours), I prefer to set a cookie that I check for (see my solution below).Finkle
C
3

We are using LightTPD as web server and i am using "mod_magnet" to do such things. Mod_magnet allows me to do request handling using simple lua scripts. We have an automatic task, which will create an empty file somewhere in the web-server's filesystem. The lua script will check for existance of this file on every request and return a static maintenance page, if the file is found. The important part of the script looks like the following:

--
-- maintenance page
--
local config = {
    ['maintenance'] = {
        ['allow'] = {
            '127.0.0.1'    -- just an example IP address
        }
    }
}

if lighty.stat(lighty.env['physical.doc-root'] .. 'maintenance') then
    tmp, _ = table.search(config.maintenance.allow, lighty.env['request.remote-ip'])
    if not tmp then
        -- maintenance modus
        lighty.content = {
            { filename = lighty.env['physical.doc-root'] .. 'error/maintenance.html' }
        }

        return 503
    end
end

In the script there's also a configuration to still let specific IP addresses through, so you can still view the website for example from your company network, when everyone else just get's the maintenance page.

This solution works very well and because of automation, it does not matter, if you need the maintenance page on one or on many webservers.

If you are interested, you can find more information regarding "mod_magnet" over here:

http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModMagnet

Corinacorine answered 16/5, 2012 at 17:10 Comment(0)
O
1

Since you mention .htaccess I'm assuming you're using Apache as your web server. As other have mentioned you can use .htaccess or you can edit the VirtualHost conf for that specific site.

Either way I would recommend using mod_rewrite as Jonathon Sampson recommended, however, you want to be very careful to make sure that you send the proper status codes as well because important bots such as GoogleBot want to see a 503 which tells them that you are currently in maintenance.

There are some pretty clear instructions for doing exactly what you want in this AskApache article.

Overwrought answered 16/5, 2012 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.