Using htaccess for scheduled downtime

Apache Webserver
Jul
29

Sometimes you may find that your site needs to be innaccessible for a short period of time whilst updates are performed, such as updates to user logins. This should not be allowed whilst the site is being edited, but you may need to be able to check the site yourself during this update. Here's how to block others, except yourself during these periods.

During periods of downtime when I am working on areas of the site that may have an adverse effect on user experience, I make sure that my .htaccess file contains the following code:

RewriteCond %{REMOTE_ADDR} !(xxx.xxx.xxx.xxx)
RewriteCond %{REQUEST_URI} !downtime.php$
RewriteRule \.(php|html) http://www.newearthonline.co.uk/downtime.php [R]

In this example I have replace my IP address with xxx.xxx.xxx.xxx for security reasons. What this example does, is that it looks at the IP address of the user requesting the page and if it isn't the IP specified then it goes on to try and match the following RewriteCond. This condition directive then checks to see if the file being requested isn't downtime.php and if true goes on to rewrite any requests containing .php or .html and sends them to the downtime.php page. This page is the page that will contain the downtime message.

If the first RewriteCond does not return true, then that means the user can access the site, the reason for this is so that if you put your own IP address in here you can still visit the site yourself to make sure what you are doing is taking effect as expected, whilst ALL other users will see the downtime page.

The second RewriteCond is there to stop downtime.php redirecting to itself and causing an infinite loop, just as the check for .php or .html is there to make sure only webpage requests are redirected, and not those for images, javascript files, CSS files, etc.

If you decide that you don't want people who accidentally bookmark the downtime page to go back to it when the site is back up, then you can check the referer for that page to make sure it's not empty - if it is then redirect back to the main page (index.php).

RewriteCond %{HTTP_REFERER} ^$
RewriteRule downtime.php /index.php [R=301]

Please note that to use the above to redirect people away from the downtime.php you will need to add an L to the list if flags for the original RewriteRule earlier. To add time into the equation, first consider the following example:

RewriteCond %{REMOTE_ADDR} !(xxx.xxx.xxx.xxx)
RewriteCond %{REQUEST_URI} !downtime.php$
RewriteCond %{TIME} >20061011073000
RewriteCond %{TIME} <20061011080000
RewriteRule \.(php|html) http://www.newearthonline.co.uk/downtime.php [R]

This example will look at the server's clock and see if it is 11th October 2006, and between the hours of 7:30am and 8:00am by testing time with the two time's I have specified.

comments - Post a comment

Anonymous

Anonymous

posted 1 years ago

Hello! great idea of color of this siyte!

Andy

Andy

posted 12 months ago

Useful stuff - I've bookmarked it, thanks

finka

finka

posted 8 months ago

grid girls poster of movie alien hot lesbianes personalized couples rings rose-breasted grosbeaks disney movies that start with e rumor has it movie review weight loss doctor mass adultfriendfinder.co cowboy mouth

Add a comment

Please feel free to leave comments! Anything that looks like spam will be removed, but other than that I won't censor anything you have to say whether it's positive or negative. Everyone has an opinion so express yourself!