IISのリダイレクトでディレクトリ内のページごとのリダイレクト先をまとめて設定する - IISのリダイレクト変数の利用 - IIS

IISのリダイレクト変数を利用して、リダイレクトするディレクトリ内のページごとのリダイレクト先をまとめて設定する方法を紹介します。

リダイレクトの変数

リダイレクトURL内に以下の変数を含めることができます。

$S

要求された URL において一致するサフィックスを渡します。一致するサフィックスとは、リダイレクト先の URL に置き換わった後もそのまま残る部分です。



上記の設定
http://www.ipentec.net/doc$S
と記述すると、当該ディレクトリへのすべてのファイルのアクセスは"http://www.ipentec.net/doc"への転送となります。ただしURLパラメーターは引き継がれません。
リダイレクト例 (http://app.ipentec.com/catcher/ap/ に設定した場合)
http://app.ipentec.com/catcher/ap/cont.html → http://www.ipentec.net/doc/cont.html
http://app.ipentec.com/catcher/ap/img/banner.jpg → http://www.ipentec.net/doc/img/banner.jpg
http://app.ipentec.com/catcher/ap/read.php?id=10 → http://www.ipentec.net/doc/read.php

$P

元の URL のパラメータを渡します。

$Q

元の URL の疑問符 (?) とパラメータの両方を渡します。

$V

要求された URL をサーバー名を除いて渡します。

$0 ~ $9

要求された URL の中で、指定されたワイルドカードに一致する部分を渡します。

!

リダイレクトをしません。

リダイレクトのワイルドカード

"*"を用いることで任意の文字列と一致させることができます。
ワイルドカードを用いる場合はWeb.configファイルを直接編集して記述します。

コード例1

拡張子がphpファイルにアクセスした際にドキュメントルートのdefault.htmlにリダイレクトする例です。
<configuration>
  <system.webServer>
    <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
      <add wildcard="*.php" destination="/default.html" />
    </httpRedirect>
  </system.webServer>
</configuration>
リダイレクト例
http://app.ipentec.com/app01/contents/pp.php → http://app.ipentec.com/default.html
http://app.ipentec.com/app01/contents/test.php → http://app.ipentec.com/default.html
http://app.ipentec.com/app01/contents/123.php → http://app.ipentec.com/default.html

コード例2

下記の例では,拡張子がhtmlファイルにアクセスした際にhttp://www.ipentec.net/index.htmlにリダイレクトする例です。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
      <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
         <add wildcard="*.html" destination="http://www.ipentec.net/index.html" />
      </httpRedirect>
    </system.webServer>
</configuration>
リダイレクト例
http://app.ipentec.com/app02/doc.html → http://www.ipentec.net/index.html
http://app.ipentec.com/app02/main.html → http://www.ipentec.net/index.html
http://app.ipentec.com/app02/index.html → http://www.ipentec.net/index.html
著者
iPentec.com の代表。ハードウェア、サーバー投資、管理などを担当。
Office 365やデータベースの記事なども担当。
最終更新日: 2024-01-06
作成日: 2018-07-31
iPentec all rights reserverd.