Redirect to an under maintenance page with .htaccess
Page: prev. | 1 | next
That a website has to occasionally be taken down while maintenance is carried out is an unavoidable aspect of running a website.
The simplest solution is to use a temporary index page in the site root explaining that the site is currently under maintenance but what of all the other pages that visitors may link to from other sites linking to it or from bookmarks? Temporarily replacing all pages is, if it can be called an option, a major undertaking. What will search engines index if they crawl by, and what of those undertaking the maintenance who may needing to access the site to test while it is down? Temporary pages here are no option at all.
Using a redirect in your websites .htaccess configuration file is the perfect and only real solution and thankfully it is quite easy to do. If you are not using an .htacccess file you can create one for use while maintenance is under way and remove it once it is completed.
What’s an .htaccess file (wikipedia.org)
First of course create the maintenance page with the under maintenance message you wish to use. This is the page to which all access attempts to any pages will be redirected.
Many you will see are simply text with no formatting, styling or images as the redirect they use redirects everything, including stylesheets and images, to the redirect page, but this is not necessary; assuming it is only the sites pages that are under maintenance you can easily still allow complete access to resources such as images, stylesheets and scripts if you wish.
This will certainly create a more pleasing under maintenance page and allow images and scripts which you allow to be hotlinked, such as JPEG site banners and CSS resources to be unaffected while maintenance is underway. Be sure to not link to any pages that will be redirected from by the maintenance.
Now place the following in your site’s root .htaccess file.
- #Site under maintenance
- RewriteEngine On
- RewriteBase /errdocs/
- RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
- RewriteCond %{REQUEST_URI} !307\.html$ [NC]
- RewriteCond %{REQUEST_URI} !\.(jpe?g|png|gif|css|ico)$ [NC]
- RewriteRule ^(.*)$ 307.html [R=307,L]
What it does.
All requests that are not for the maintenance page or for jpeg, jpg, png, gif, css or ico files will be redirected to the maintenance page. Here the maintenance page is assumed to be in a sub-directory of the root path named errdocs and is named 307.html, matching the Temporary Redirect HTTP standard response, the code which will be used when redirecting (302 could also be used as search engines treat them pretty much equally). You can change the name of the page you wish to redirect to by change the two occurrences of 307.html, but be sure to follow any instructions on escaping characters in each line.
Here’s a line-by-line break down.
Line 2: the RewriteEngine directive enables the runtime rewriting engine and is required.
Line 3: the RewriteBase directive specifies a directory added to the root path to prefix any relative paths following in this particular rule.
You can change this to RewriteBase /
if your maintenance page is in the root directory with the .htaccess file or indded could remove the whole
line as the path will be defaultly assumed to be the root.
Line 4: the first RewriteCond directive attempts to match
the IP address of the request with an IP address listed which is will be the IP address of the webmaster undertaking maintenance. This will allow
requests from this IP address to access all pages as usual and not be redirected. A dummy IP address (11.111.111.111
) is used here
and this needs to be replaced with a real IP address
(what’s your IP address?). Each octet of an IP address is separated by a period character and
in RewriteCond directives period characters must be escaped with a preceding backslash (\
) character as they have special meaning.
If you do not need a particular IP address to be able to access the site normally then simply remove this condition by removing this line.
Line 5: the RewriteCond directive here matches any requested files that are not the maintenance page 307.html (remember the RewriteBase
directive in line 3 actually makes this /errdocs/307\.html$
). It is again important to escape the period character in the condition
with a backslash character (so .html becomes \.html). The [NC]
flag causes the filename to be case insensitive (307.html or 307.HTML will match).
The $
character ensures that html is at the end of the filename. If your site uses php or as asp pages rather than html, then you
can change \.html$ to \.php$, \.asp$, or \.aspx$ as required.
Line 6: the RewriteCond directive here matches any requested files that are not of the extensions listed in parentheses separated with a pipe
(|
) character. A ?
wildcard character is used to match JPEG files, the ? matching one occurrence of the previous
character (e) if it exists or if it does not, allowing both jpeg and jpg to match. You can add other file extensions you wish to allow (js for
javascript files for example).
Line 7: finally the RewriteRule directive is applied only if both conditions in lines 3 and 4 are met and the wildcard string
^(.*)$
redirects any request to the maintenance page. RewriteBase is applied, so it actually redirects to /errdocs/307.html. The
period character in this line does not need to be escaped as it did in the condition in line 5. The [R=307,L]
flag passes the 307
Temporary Redirect HTTP standard response code and L calls an end to the rule.
And that’s it: simply leave your .htaccess file with that code in place until maintenance is over and simply remove the .htaccess file or the redirect code from it when all is done. I hope that makes maintenance less of a headache in future.
Notes:
• An htaccess file can be a tricky thing, and if you are new to creating one, please read up a bit first as errors can tie-up your site until fixed. Here is a great guide to creating an htaccess file: Comprehensive guide to .htaccess.
Related entries
Page: prev. | 1 | next