Quantcast
Channel: 64 Blog
Viewing all articles
Browse latest Browse all 45

PowerShell を使って Web.Config に設定された URLRewrite の設定を編集する

$
0
0

Web.Config を編集することで URLRewrite の規則を変更することが出来ますよね。

メンテナンスなどの繰り返し行う作業では、スクリプトなどを用意しておくと便利だと思うので、今回 PowerShell を使って URLRewrite の規則部分を書き換えてみたいと思います。

 

例えば、下記のような Web.Config において、受信規則:ReverseProxyInboundRule1 のURLの値を書き換えるには…

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <outboundRules>
                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://www.example.com/(.*)" />
                    <action type="Rewrite" value="http{R:1}://www.gine.jp/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
        <urlCompression doDynamicCompression="false" />
    </system.webServer>
</configuration>

 

下記のようなコードで編集することが出来ます。

$ruleName = "ReverseProxyInboundRule1";
$value = "http://hogehoge/{R:1}";
$cfg = "c:\inetpub\wwwroot\web.config";

$xml = [xml](Get-Content $cfg -Encoding UTF8);
$rule = $xml.configuration."system.webServer".rewrite.rules.rule | where { $_.name -eq $ruleName }
$rule.action.SetAttribute("url", $value);

$xml.save($cfg);

Viewing all articles
Browse latest Browse all 45

Trending Articles