0
2

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 3 years have passed since last update.

Apache 2.4 リダイレクト設定 チートシート

Last updated at Posted at 2020-08-16

Apache 2.4のRewriteCond, RewriteRuleのチートシート。
前提条件として、RewriteEngine Onになっていることを確認する。

例えば↓の感じで使える。
この例では、192.168.100のIPレンジ以外のアクセスを全てメンテナンスページへリダイレクトする。

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^(192.168.100)$
RewriteRule ^(.*)$ http://example.com/maintenance.html [R=503, L]

以下、詳細です。

RewriteCondの書式

RewriteCond %変数名 条件 [フラグ]

フラグは次の種類がある。

[OR]
[AND]
[NC] 大文字小文字を区別しない

デフォルトのフラグは[AND]が適用される。
[OR]のほうが[AND]より先に評価される点に注意。

RewriteRule

RewriteRuleはファーストマッチで評価される。
フラグの細かい話は次の通り。

  • R=301
    • Ridirect 301の略
    • 301の恒久リダイレクトをする
  • L
    • Lastの略
    • マッチしたら以降のルールは無視する
  • R
    • Redirectの略
    • 302のリダイレクトをする

HTTPS化したい場合の例

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/%{REQUEST_URI} [R=301, L]

URI正規化したい場合の例

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301, L]
0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?