If you’re going to have to stop your site for a maintenance or deploy break and care about your users (and SEO!), you should setup a custom 503 page. AskApache provides some documentation on this, but the examples require creating a PHP page. One of the commenters proposed a non-PHP version, but it’s complicated and didn’t work for me.
So, this is the simplest way to set-up a custom “we’ll be back shortly” error page in Apache 2 that also tells Googlebots to retry after 1 hour:
ErrorDocument 503 /503.html RewriteEngine On RewriteCond %{REQUEST_URI} !^/503.html$ Header always set Retry-After "3600" RewriteRule .* - [R=503]
Read on for explanation and a more advanced example.
The above lines should be placed in you Apache’s site config file (e.g. /etc/apache2/vhosts.d/<your-site-name>_vhost.conf
) somewhere under DocumentRoot
directive. The first line sets a custom file for HTTP 503 status. The path you provide here is relative to DocumentRoot
, so if you have:
DocumentRoot /var/sites/my_site/current/public ErrorDocument 503 /503.html
then the file should be at /var/sites/my_site/current/public/503.html
. Having a regular HTML means that you do not need anything except Apache itself to serve it. Inside it you can provide a message about the cause of the break and when you expect your site to be back up.
Next line (line #2 in first example) turns the RewriteEngine on, which is needed to process the following directives. Line #3 excludes from processing all the requests that go to our 503 page. Without it we would get an endless loop of redirections (which would not be endless in Apache, of course, but after exceeding the redirection limit would cause an error instead of producing the page).
Line #4 sets Retry-After header for bots, which is set here for 3600 seconds. If you expect your break to be much shorter, you can use a lower value. The last line sets a 503 response for all requests that made it up to here (i.e. not excluded by line #2).
Let the developers in
Often you need to allow some people (the developers or the client) to play with site even if it’s unavailable to the rest of the Internet. To do so, you can exclude them from getting the 503 page by entering their IPs:
ErrorDocument 503 /503.html RewriteEngine On RewriteCond %{REMOTE_ADDR} !^1.2.3.4$ RewriteCond %{REQUEST_URI} !^/503.html$ Header always set Retry-After "3600" RewriteRule .* - [R=503]
The only thing new here is line #3, which excludes all requests coming from IP 1.2.3.4. You can use this directive as many times as you need.
Troubleshooting
If the above doesn’t work, first try following:
- Double-check the 503 file location.
- Assure that the owner of apache process can access and read this file.
- Make sure that you have mod_headers and mod_rewrite activated.
August 18th, 2009 at 17:57:50
Great! Thanks for sharing!
August 18th, 2009 at 18:09:21
FWIW, my maintenance page had some images which were not displaying as they were caught in the RewriteRule, so I changed it to:
RewriteRule !\.(ico|gif|jpg|png|css)$ – [R=503]
to allow those extensions to still be served.
August 6th, 2010 at 15:49:06
Another ‘thanks’ post here :)
June 22nd, 2011 at 23:20:05
Thanks for this, and thanks to Ernesto for the updated rule (a reminder that you need to free those things up if your page is going to be styled at all using your page’s standard template)
April 10th, 2013 at 22:31:01
I use this rewrite condition to allow access to anything static to be able to reach stylesheets, images, etc much like Ernesto, but easily allow for anything that does not need to be processed by my application.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* – [R=503]
May 23rd, 2013 at 16:32:44
[…] If you have a planned downtime you can setup a 503 code which tells the search engines that you’re aware of the situation and that they should try again later. You can even set a return time for the search engines to crawl your site. This is pretty technical for the average webmaster but you can see more details from the Google blog. Another technical resource is this post. […]
May 27th, 2013 at 13:46:10
[…] If you have a planned downtime you can setup a 503 code which tells the search engines that you’re aware of the situation and that they should try again later. You can even set a return time for the search engines to crawl your site. This is pretty technical for the average webmaster but you can see more details from the Google blog. Another technical resource is this post. […]