LoginSignup
0
1

More than 5 years have passed since last update.

AppCmd.exeでURL書き換えの設定

Last updated at Posted at 2018-06-29

はじめに

HTTPでアクセスされた場合に、HTTPSにリダイレクトするという要件がありました。
また、GUIではなく、コマンドラインで自動化する必要がありました。
そこでIISに対して URL書き換え(URL Rewrite)の設定を、AppCmd.exe で施すことにしました。
初めて使う AppCmd.exe で苦戦しましたので、自分用メモとして残しておきます。

開発環境

IIS 7.0以降
(今回、Windows 10 + IIS 10.0、Windows Server 2012 R2 + IIS 8.5 で確認)

セットアップ

IISのインストール
URL rewite 2.0のインストール

URL rewite 2.0のインストールについて

URL書き換え(URL Rewrite)が IIS のGUI上にない場合、以下の要領でインストールします。

1.Web Platform Installer をインストールします
Web Platform Installer - Microsoft IIS.NET

2.URL rewite 2.0をインストールします

  1. Web Platform Installer を起動
  2. 検索テキストボックスに "URL Rewrite" と入力
  3. URL Rewrite 2.0 の『追加』ボタンをクリック
  4. 『インストール』ボタンをクリック

サンプル

IIS 構成データにアクセスするには、管理者特権が必要です。

#AppCmd.exe は %systemroot%¥system32¥inetsrv¥ ディレクトリにあります。
#このディレクトリは自動的には PATH のパスにならないので、コマンドを実行するときには、
#この実行可能ファイルへの完全なパスを使用する必要があります。
#参考:https://technet.microsoft.com/ja-jp/library/dd647592.aspx
$systemPath = [Environment]::GetFolderPath('System')
$cmd = Join-Path $systemPath inetsrv\AppCmd.exe

#サイトの情報
$website = Get-Website
$siteName = $website.name

#HTTPにアクセスされた場合、HTTPSにリダイレクトする
& $cmd set config $siteName -section:system.webServer/rewrite/rules /+"[name='http_redirect',enabled='True']" /commit:apphost
& $cmd set config $siteName -section:system.webServer/rewrite/rules /"[name='http_redirect']".match.url:"(.*)" /commit:apphost
& $cmd set config $siteName -section:system.webServer/rewrite/rules /+"[name='http_redirect'].conditions.[input='{HTTPS}',pattern='^OFF$']" /commit:apphost
& $cmd set config $siteName -section:system.webServer/rewrite/rules /"[name='http_redirect']".action.type:"Redirect" /commit:apphost
& $cmd set config $siteName -section:system.webServer/rewrite/rules /"[name='http_redirect']".action.url:"https://{HTTP_HOST}/{R:1}" /commit:apphost
& $cmd set config $siteName -section:system.webServer/rewrite/rules /"[name='http_redirect']".action.redirectType:"Permanent" /commit:apphost

参考サイト

http を https へリダイレクト - Windows Server Essentials を中心とした雑記
HTTPからHTTPSへリダイレクト - とある会社の監査証跡
AppCmd.exe の使用の開始 - Microsoft TechNet
URL Rewrite Module Configuration Reference - Microsoft Docs
AppCmd を使用して WebDAV 設定を構成する方法 - Microsoft TechNet
Change rule conditions with appcmd - Microsoft IIS.NET Forums
UrlRewrite via appcmd - stackoverflow
IIS関連ファイルの場所 - Windows-Maniax

0
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
0
1