1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Apache2.0.X系で.htaccessによる時間制限のRewrite設定したときのメモ

Last updated at Posted at 2019-01-24

はじめに

Apache2.0.X系で.htaccessによる時間制限のRewrite設定したとき、はまったのでメモします。

以下、15:00~22:00のみアクセス許可を例に記載しています。

結論:動いた設定

最初に結論で、以下の設定が期待値どおりに動きました。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{TIME_HOUR}%{TIME_MIN} <1500 [OR]
  RewriteCond %{TIME_HOUR}%{TIME_MIN} >2200
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteRule ^.*$ /maintenance.html [R,L]
</IfModule>

エラーになったケース

エラーその1:RewriteRule: invalid HTTP response code for flag 'R'

最初、時間制限をどうやって書くのか、といういことで、
https://blog.shnr.net/?p=165
こちらのサイトを参考にしていたのですが、
error_logに"RewriteRule: invalid HTTP response code for flag 'R'"と
出力されてしまいました。

ErrorDocument 503 /maintenance.html
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{TIME_HOUR}%{TIME_MIN} <1500 [OR]
  RewriteCond %{TIME_HOUR}%{TIME_MIN} >2200
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteRule ^.*$ - [R=503,L]
</IfModule>

エラーその2:Unsupported HTTP response code 310

次に
https://oki2a24.com/2013/10/14/cannot-503-specify-response-status-code-in-apache-2-0-64/
こちらのサイトをみて、
Apache2.0.X系は300~400を指定すると書いてあったので、
レスポンスコードを変えたら動くかと期待したら、
error_logに"Unsupported HTTP response code 310"と
出力されてしまいました。

ErrorDocument 310 /maintenance.html
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{TIME_HOUR}%{TIME_MIN} <1500 [OR]
  RewriteCond %{TIME_HOUR}%{TIME_MIN} >2200
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteRule ^.*$ - [R=310,L]
</IfModule>

エラーその3:301~307ではエラーはでないが、時間外にみれてしまう。

で、301,302...と順々に変更したら、エラーはでないが、
時間外でもサイトがみれてしまう。
308以降を使うと、"Unsupported HTTP response code"になることがわかる。

ErrorDocument 301 /maintenance.html
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{TIME_HOUR}%{TIME_MIN} <1500 [OR]
  RewriteCond %{TIME_HOUR}%{TIME_MIN} >2200
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteRule ^.*$ - [R=301,L]
</IfModule>

おわりに

偶然、結論の書き方をしたら、期待通りの動きをしたのですが、
ぐぐっても古すぎる情報を検索するのは困難なので、
Apacheのバージョンあげてほしいものです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?