Windowsサーバでホームページを作成する案件があり、wwwのある無しやSSLのリダイレクト設定をする必要がありました。その際に実施した、「web.config」での設定方法をご紹介します。
-目次-
サーバ環境
今回はWindowsサーバでサイト構築をする必要があったため、共用のWindowsサーバプランがあるSPPDを利用しました。
- サービス名:SPPDレンタルサーバー
- プラン:Windows共用サーバー ビジネスシリーズ ライトプラン
Windowsサーバで利用されているWebサーバ「IIS」環境では「.htaccess」ではなく「web.config」ファイルを利用する
リダイレクト設定をする際は、一般的なUNIX系サーバでは、Webサーバ「Apache(アパッチ)」環境下で「.htaccess」ファイルを編集し、リダイレクト設定をすることが多いと思います。
Windowsサーバでは、IIS環境下で「web.config」ファイルを設定することで、リダイレクト設定やパスワード制限をかけることが可能になります。
「.htaccess」ファイル同様、通常は「web.config」ファイルをルート直下に設置しますが、ディレクトリを指定して、設置ディレクトリ以下に範囲を限定することも可能です。
web.configの設定例
wwwある無しのリダイレクト設定
「www」無しにする場合の例ですが、例えば「www.●●●.com」から「●●●.com」にリダイレクト設定する際はファイル名を「web.config」として以下の内容を記載し、ドメインのルートディレクトリにアップロードします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?phpxml version="1.0" encoding="UTF-8" ?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Redirect001" stopProcessing="true"> <match url="(.*)"/> <conditions> <add input="{HTTP_HOST}" pattern="^www\.●●●\.com$" /> </conditions> <action type="Redirect" url="http://●●●.com/{R:1}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration> |
特定のディレクトリ以下をSSL(https)接続にリダイレクトする設定
SSL暗号化通信(https)をする際に、ドメイン全体ではなく、例えばメールフォームがあるお問い合わせページのディレクトリのみ、「http」でアクセスがあった際、必ず「https」でのSSL接続をさせたい、というような場合は以下のようにリダイレクト設定をします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?phpxml version="1.0" encoding="UTF-8" ?> <configuration> <system.webServer> <rewrite> <rules> <rule name="RedirectHTTPS" enabled="true"> <match url="(●●●(SSL接続したいディレクトリ名)/.*)" ignoreCase="false" /> <conditions> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration> |
www無しに統一、かつ、特定のディレクトリ以下をSSL接続させるためのリダイレクト設定
前述の二つのリダイレクト設定を同時に設定したい場合は、「<rules>」タグの間に、設定したい内容を連続して記載します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?phpxml version="1.0" encoding="UTF-8" ?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Redirect001" stopProcessing="true"> <match url="(.*)"/> <conditions> <add input="{HTTP_HOST}" pattern="^www\.●●●\.com$" /> </conditions> <action type="Redirect" url="http://●●●.com/{R:1}" redirectType="Permanent" /> </rule> <rule name="RedirectHTTPS" enabled="true"> <match url="(●●●(SSL接続したいディレクトリ名)/.*)" ignoreCase="false" /> <conditions> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration> |
リダイレクト設定した際は、きちんとアクセスできるか入念にチェックして問題無ければ完了です。