htaccessによるリダイレクト設定


1. メンテナンス画面の表示

特定のIPアドレスを除外するサンプルです。
また、メンテナンス画面自体のファイルおよび画像ファイルをメンテナンス画面へジャンプさせないようにしています。
(1) 通常の設定(レスポンスコード503を返す)
ErrorDocument 503 /maintenance.html
 
<IfModule mod_setenvif.c>
  SetEnvIf Remote_Addr "^192.168." localip
  SetEnvIf Remote_Addr "^172.(1[6-9]|2[0-9]|3[0-1])." localip
</IfModule>
 
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
 
  RewriteCond %{REQUEST_FILENAME} !.(gif|jpg|png|js|css)$
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteCond %{REMOTE_ADDR} !^10\.
  RewriteCond %{REMOTE_ADDR} !=xxx.xxx.xxx.xx
  RewriteCond %{ENV:localip} !=1
  RewriteRule ^.*$ - [R=503,L]
</IfModule>

(2) IPの範囲指定の場合
例えば「10.123.45.67/28」の場合、「10.123.45.67~10.123.45.82」までが範囲となるため、以下のように記述します。

ErrorDocument 503 /maintenance.html
 
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !.(gif|jpg|png|js|css)$
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteCond %{REMOTE_ADDR} !^10\.123\.45\.6[7-9]$
  RewriteCond %{REMOTE_ADDR} !^10\.123\.45\.7[0-9]$
  RewriteCond %{REMOTE_ADDR} !^10\.123\.45\.8[0-2]$
  RewriteRule ^.*$ - [R=503,L]
</IfModule>

(3) 参照元IPを取得する方法
WAFやLoadBalancer等でIPアドレスが変換されてしまう場合に参照元IPアドレスを取得する方法です。
ErrorDocument 503 /maintenance.html
 
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !.(gif|jpg|png|js|css)$
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteCond %{HTTP:X-Forwarded-For} !^***\.***\.***\.***$
  RewriteRule ^.*$ - [R=503,L]
</IfModule>


(4) 特定のフォルダのみ503にする
「recruit」フォルダのみ503画面にする
ErrorDocument 503 /maintenance.html
 
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !.(gif|jpg|png|js|css)$
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteCond %{REMOTE_ADDR} !^***\.***\.***\.***
  RewriteRule ^(recruit/.*)$ - [R=503,L]
</IfModule>

(5) 監視等のためレスポンスコード503にしたくない場合
ErrorDocument 503 /maintenance.html
 
<IfModule mod_setenvif.c>
  SetEnvIf Remote_Addr "^192.168." localip
  SetEnvIf Remote_Addr "^172.(1[6-9]|2[0-9]|3[0-1])." localip
</IfModule>
 
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !.(gif|jpg|png|js|css)$
  RewriteCond %{REQUEST_URI} !=/maintenance.html
  RewriteCond %{REMOTE_ADDR} !=xxx.xxx.xxx.xx
  RewriteCond %{ENV:localip} !=1
  RewriteRule ^.*$ /maintenance.html [L]
</IfModule>
※metaタグに「noindex」等を記述するなど、検索エンジンクローラ対策をしないと、検索エンジンにメンテナンスページが登録されてしまいますので、注意を要します。

2. NOT FOUND(404)エラー設定

(1) 基本設定
ErrorDocument 404 /404.html

(2) 403エラーを404エラーに変換
セキュリティ上ファイルやディレクトリがアクセスできないまでも、存在することを検知されることを避けるため、403エラーを404エラーに変換します。
ErrorDocument 403 -
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule .* - [R=404,L]
</IfModule>
※httpd.confに記載する場合には「RewriteBase /」行を削除すること。

(3) PHPファイルでErrorDocumentに設定したファイルにジャンプしない場合
PHPファイルで404エラーになった場合に、PHP自体でFile Not Foundエラーを発生してそこで停止してしまい、ErrorDocumentに指定したファイルが表示できない場合があります。
ErrorDocument 404 /404.html
 
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
 
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /404.html [L]
</IfModule>

3. リダイレクト設定

(1) 特定のIPアドレスを除外して別ホストへリダイレクト
www.example.comへアクセスしてきた場合にはwww.example.jpへリダイレクトさせます。
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REMOTE_ADDR} !^***.***.**.**$
  RewriteCond %{REMOTE_ADDR} !^***.***.**.**$
  RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
  RewriteRule ^.*$ https://www.example.jp%{REQUEST_URI} [R=301,L]
</IfModule>

(2) http→httpsへのリダイレクト
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

(3) wwwなし→wwwありへのリダイレクト
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^.*$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>


(4) ディレクトリ名を変更
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^old(/.*)$ /new$1  [R=301,L]
</IfModule>

(5) ホスト名を変更してリダイレクト
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.example\.jp [NC]
RewriteRule ^.*$ https://new.example.jp%{REQUEST_URI} [R=301,L]
</IfModule>

(6) 自身のURLのクエリ文字のみ削除する場合
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{QUERY_STRING} !=""
  RewriteRule ^(.*)$ $1? [R,L]
</IfModule>
※URLの末尾に「?」を記述するとクエリ文字が削除されます。

なお、Apache2.4では、新しく「QSD」フラグが追加されました。上記と同じ結果になります。
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{QUERY_STRING} !=""
  RewriteRule ^(.*)$ $1 [R,L,QSD]
</IfModule>

(7) index.htmlおよびindex.html?xx=xxを/にリダイレクト
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^(.*)/index.html /$1? [R,L]
  RewriteRule ^index.html /? [R,L]
</IfModule>

(8) 特定のページのみ除外
例えば「/seminar/」フォルダにアクセスした場合トップページへ移動するが、特定のセミナーページのみ別ページにリダイレクトする場合
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^seminar/9.html /seminar/tokyo.html [R=301,L]
  RewriteCond %{REQUEST_URI} !seminar\/tokyo\.html$
  RewriteRule ^seminar / [R=301,L]
</IfModule>
※リダイレクト後のURLを除外しないと、一旦「tokyo.html」にリダイレクトした後に、再度トップページにリダイレクトしてしまうので注意

(9) クエリ文字付きURLを固定ページへ移動
クエリ文字付きURLにおいて、パラメータの値によってそれぞれ別の固定ページURLにリダイレクトする場合
【例】
(転送元)/public/index.php?do=detail&serial=11
(転送先)/support/faq5.html
 
(転送元)/public/index.php?do=detail&serial=22
(転送先)/support/faq7.html

この場合には、環境変数に一度パラメータの値を保存して、分岐します。
RewriteCond %{QUERY_STRING} (^|&)serial=([0-9]+)($|&)
RewriteRule .* - [E=X_QUERY_STR:%2]
 
RewriteCond %{ENV:X_QUERY_STR} ^11$
RewriteRule ^public/index.php /support/faq5.html? [R=301,L]
RewriteCond %{ENV:X_QUERY_STR} ^22$
RewriteRule ^public/index.php /support/faq7.html? [R=301,L]
※URLの末尾に「?」を記述するとクエリ文字が削除されます。

(10) サブディレクトリをルートとしてアクセスする場合。
例えば、「/test/index.html」を「/index.html」としてアクセスしたい場合は、以下のように指定します。
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/test
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /test/$1 [L]
</IfModule>

(11) ルートディレクトリのアクセスをサブディレクトリにリダイレクトする場合。
[1] URLの書き換えを行わない場合
「/」でアクセスしたら、「/ja」の内容を表示する場合。
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule ^$ ja/ [L]
</IfModule>

[2] URLの書き換えを行う場合
「/」の場合は「/ja」にリダイレクトし、「/en」の場合はそのまま表示する場合。
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/ja/?
    RewriteCond %{REQUEST_URI} !^/en/?
    RewriteRule ^(.*)$ ja/$1 [R=302,L]
</IfModule>
※上記の場合は302リダイレクトにすると、検索エンジンには「/」と登録されます。301にすると、「/ja/」と登録されますので、ケースによって使い分けて下さい。

(12) サブディレクトリにファイルがなかったら、サブディレクトリトップを表示
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_URI} ^/sougou/seika/ [OR]
  RewriteCond %{REQUEST_URI} ^/sougou/annai/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*)/ /$1? [R,L]
</IfModule>

(13) 重複しているスラッシュを1つにする場合
URLにおいて例えば、「/products//」や「/about//index.html」などの重複しているスラッシュを1つにするリダイレクトです。
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
 
  RewriteCond %{THE_REQUEST} ^.+//+
  RewriteRule ^(.*)/(.*) /$2/$1 [R=301,L]
</IfModule>

(14) WordPress用
# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
# END WordPress

(15) WebRelease2用
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
 
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /WebRelease2/ [R=301,L]
 
    RewriteRule ^$ /WebRelease2/ [R=301,L]
 
</IfModule>

(15) 拡張子の変更
.phpを.htmlに書き換えるものです。
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)\.php $1.html [R,L]
</IfModule>

4. RewriteEngine Onでエラーが発生する場合

リダイレクト設定で403エラーが発生する場合には、ディレクトリのFollowSymLinksのオーバーライドが禁止になっている場合があります。
その場合には、まずFollowSymLinksの許可を与える必要があります。
Options +FollowSymLinks 
 
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REMOTE_ADDR} !=xxx.xxx.xxx.xx
  RewriteRule ^.*$ http://www.exampel.com [R=301,L]
</IfModule>


最終更新:2024年05月21日 15:47