Rewrite rule for "site down" pages
Asked Answered
V

5

5

I was trying to install this .htaccess to notify my users of site maintenance. It seems the first [L] isn't working and the second rewrite is doing everything.

How do you guys do site maintenance messages?

RewriteEngine on

RewriteRule ^s/down$ index.html [L]
RewriteRule ^(.*)$ http://metaward.com/s/down [R=302,L]
Vassily answered 26/8, 2009 at 22:14 Comment(0)
V
5

This seems to work (but I have to set the status code in PHP)

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/static/.*$
RewriteCond %{REQUEST_URI} !^/media/.*$
RewriteRule .* down.php [L]

and in down.php

<?php
header('HTTP/1.1 503 Service Temporarily Unavailable',true,503);
?>

Any problems with this? My main concerns are what user's see (which is why i keep static content) and what search engines see (the 503 status code).

Vassily answered 26/8, 2009 at 22:34 Comment(1)
This is what I settled on, but RewriteConds are not needed. In this case RewriteRule !^/(?:static|media)/ down.php [L] will do.Termless
M
6

You don’t need to an external redirect. Just send the 503 status code and your error document.

RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteRule ^(.*)$ /503.html [R=503,L]
ErrorDocument 503 /503.html

But you need Apache 2.x to use a different status code with the R flag other than 3xx.

Mweru answered 26/8, 2009 at 22:19 Comment(5)
503 won't actually cause the browser to redirect.Vassily
@Paul Tarjan: I’ve never said that. Apache 2 just extended the use of the R flag for setting status codes. Only 3xx status codes will actually cause an external redirect.Mweru
Setting [R=503] shows a default apache page not the one I specified in my RewriteRule. Is this the expected response?Vassily
Add "ErrorDocument 503 /down/index.html" to facility custom 503 message. Add "Alias /down /var/somewere/down" to share message pageMisogamy
ErrorDocument, not RewriteDocument.Scourge
V
5

This seems to work (but I have to set the status code in PHP)

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/static/.*$
RewriteCond %{REQUEST_URI} !^/media/.*$
RewriteRule .* down.php [L]

and in down.php

<?php
header('HTTP/1.1 503 Service Temporarily Unavailable',true,503);
?>

Any problems with this? My main concerns are what user's see (which is why i keep static content) and what search engines see (the 503 status code).

Vassily answered 26/8, 2009 at 22:34 Comment(1)
This is what I settled on, but RewriteConds are not needed. In this case RewriteRule !^/(?:static|media)/ down.php [L] will do.Termless
B
1

The RewriteRules I'm using on my website when I want to shut it down for maintenance are these ones :

RewriteCond %{REMOTE_ADDR} !=MY_IP_ADDRESS
RewriteRule    ^$  /down.html  [L]
RewriteCond %{REMOTE_ADDR} !=MY_IP_ADDRESS
RewriteRule    [^/down.html$]  /down.html  [L]

(Maybe not quite "optimized", I should say... But it worked (or so it seemed) each time I used those)

Everything but down.html gets redirected to down.html -- except for me, of course : I want to be able to test the maintenance operations I'm doing, obviously

ANd when I'm finished, I just comment those four lines.

Britishism answered 26/8, 2009 at 22:17 Comment(5)
The IP address condition: neat.Nonstriated
won't that make search engines index your page as if it was perfectly up but serving your weird down page?Vassily
@Paul : I have to admit I don't know for sure ; I've put <meta name="robots" content="noindex, follow" /> in down.html as a security measure, but I'm not sure what a bot would do : I've never seen the content of my down.html page on google, that's for sure, and I've never noticed anything "bad" for the other pages ;; but my maintenance operations are rare, and never lasted more than 3 hours ;; and my website is not big/famous, so not indexed too often... ;; Still, if someone sees a way to make this better, I'm definitly interested (would be nice to provide an answer, and get one myself !) ;-)Britishism
You should add the [R=503] status to the RewriteRule to signify a temporary maintenance condition.Jiggerypokery
@MartijnHeemels: [R=503] doesn't work on my Apache, because it requires ErrorDocument 503 directive. With ErrorDocument Apache behaves a little differently to what was suggested above, by redirecting user to down.html rather than just showing down.html for any request.Gillead
M
0

I use Apache as a proxy for a ruby on rails application.

The only thing I had to do was add

ProxyPass /custom-errors !
ErrorDocument 503 /custom-errors/maintenance-message.html

To my httpd.conf and make sure that : [Apache installation]/htdocs/custom-errors/maintenance-message.html exists.

Melda answered 9/12, 2014 at 20:16 Comment(0)
R
0

I don't see anyone using the Retry-After header. I've read (but don't have the link anymore as I read it a while ago) that you should use a 503 in combo with a Retry-After n (n = number of seconds to try again in; 3600 is an hour) header. If you use the 503 and the Retry-After in combo, especially on your robots.txt/sitemap it should not mess with links and page rank for SEO if you don't leave it like that for a long time.

Rothmuller answered 5/5, 2015 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.