2
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.

指定した拡張子のみ、特定のHTTPレスポンスヘッダを追加する方法

Last updated at Posted at 2014-12-24

以下、URLが「rss.xml 」で終わるときのみ、
「Access-Control-Allow-Origin : * 」をレスポンスヘッダに設定する例です。
正規表現で特定の拡張子のみレスポンスヘッダを書き換えるのに使ってます。

設定例.config
</configuration>
  </system.webServer>
(中略)
    <rewrite>
      <outboundRules>
        <rule name="カスタムヘッダ(RSS出力)">
          <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="rss\.xml$" />
          </conditions>
          <action type="Rewrite" value="*"/>
        </rule>
      </outboundRules>
    </rewrite>
(中略)
  </system.webServer>
</configuration>

設定する個所を[[***hoge***]]とすると、

web.config
</configuration>
  </system.webServer>
(中略)
    <rewrite>
      <outboundRules>
        <rule name="カスタムヘッダ">
          <match serverVariable="[[***追加したいHTTPレスポンスヘッダ***]]" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="[[***対象のファイル***]]" />
          </conditions>
          <action type="Rewrite" value="[[***追加したいHTTPレスポンスヘッダの値***]]"/>
        </rule>
      </outboundRules>
    </rewrite>
(中略)
  </system.webServer>
</configuration>

・追加したいHTTPレスポンスヘッダ
 1.追加したいレスポンスヘッダのハイフン(-)をアンダーバー(_)に置き換える
 2.頭に RESPONSE_をつける
 Access-Control-Allow-Originの場合
 ⇒ RESPONSE_Access_Control_Allow_Origin

・対象のファイル
 rss\.xml$:URLが rss.xml で終わる場合の指定
 \.xml:拡張子がXMLの場合の指定(URLが .xml で終わる)

・追加したいHTTPレスポンスヘッダの値
 レスポンスヘッダの値として出力したい任意の値を設定してください

参考1:IIS Rewrite モジュール2.0 の設定(公式)
   「Accessing Response Headers from Rewrite Rules」の項を参照
参考2:[stackoverflow.com] *.xml.gzでのみ「X-Robots-Tag」を有効にしたい

2
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
2
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?