Simple 'Down for Maintenance' Solution for Apache using .htaccess
I finally decided to create a maintenance page to use whenever I’m upgrading any of my sites. This solution was pretty simple. I added two RewriteCond stanzas to my .htaccess, at the top of the file at the base of the site. The first rewrite handles the case where the maintenance page exists:``` # If the maintenance.html page exists, redirect to that. RewriteCond /full/path/to/site/system/maintenance.html -f
Don’t redirect if the maintenance page was requested.
RewriteCond %{REQUEST_URI} !/system/maintenance.html
RewriteRule ^.*$ http://blog\.coredump\.ca/system/maintenance.html [R,L]
If the maintenance.html file exists, it is displayed. What happens when the maintenance is complete? Well, I remove the maintenance.html page of course. To prevent anyone from seeing a 404 once maintenance.html has been removed, I added the following to .htaccess immediately after the previous stanza:
# If the maintenance.html page does not exist, redirect to the index.
RewriteCond /full/path/to/site/system/maintenance.html !-f
RewriteCond %{REQUEST_URI} /system/maintenance.html
RewriteRule ^.*$ http://blog\.coredump\.ca [R,L]
Now all I do whenever I bring the site down for maintenance is:
- copy maintenance.html into the appropriate location;
- perform the site maintenance;
- remove maintenance.html.
Isn’t that easy?