Apache2.0で503メンテナンス中ページを作る

500 Internal Server Error
.htaccess: RewriteCond: bad flag delimiters
.htaccess: RewriteRule: invalid HTTP response code for flag 'R'
PHP Fatal error:  Call to undefined function: http_response_code()

に遭遇したのでメモ

古いレンタルサーバーでステータスコード503を返すメンテナンス中ページを作ろうとして.htaccessを作成したら、Apache2.0では[R=503,L]が使えずエラーになるので以下のようにした。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !\.(css|js|jpg|gif|png)$
RewriteCond %{REQUEST_URI} !=/maintenance/maintenance.php
RewriteCond %{REQUEST_URI} !=/ok.html
RewriteCond %{REQUEST_URI} !=/ok/ok.html
RewriteRule ^.*$ /maintenance/maintenance.php [L]

/ok.html、css、js、画像ファイルは表示され、それ以外のページはURLはそのままリダイレクトされずに、/maintenance/maintenance.phpの中身が表示されるようになる。

このままだと、ステータスコードが200になるのでmaintenance.phpに503を追加する。

<?php
	header("HTTP/1.0 503 Service Unavailable");
?>

maintenance.php内では<img src="/logo.png">絶対パスで記入すること。

.htaccess: RewriteCond: bad flag delimiters

RewriteCond %{REQUEST_URI} != /ok.html

!=の後にスペースを入れるとbad flag delimitersが発生するので注意。