2026年9月23日

2026年9月23日

WordPressのweb.configでのURL書き換えエラー

はじめに

WordPressサイトをWindows IIS上でホストしている際に、URLのきれいな表示やSEO最適化のために「web.config」ファイルを利用することがあります。しかし、この設定を行う際にはいくつかのエラーが発生しやすくなります。本記事では、そのようなエラーの具体的な症状と対策について詳しく説明します。

症状・背景

  • IIS上のWordPressサイトでURLリライト機能を使用しているが表示が正しくない。
  • 「web.config」ファイルを編集した後、サイトにアクセスできなくなった(白い画面)。
  • ファイルの404エラーが頻繁に発生する。
  • .htaccessからweb.configへの変換ツールを使用したが、予期しない結果となった。

手順・設定方法

ステップ1: web.configファイルを正しく編集する

# WordPress用の基本的なリライトルールを追加します。
<rule name="wordpress" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.php" />
</rule>

# .htaccessからのリライトルールをweb.configに変換します。
<rewrite>
    <rules>
        <rule name="WordPress Rule 1" stopProcessing="true">
            <match url="^index\.php$" ignoreCase="false" />
            <action type="None" />
        </rule>
        <rule name="WordPress Rule 2" stopProcessing="true">
            <match url="^wp-(content|admin|includes).+" ignoreCase="false" />
            <action type="Rewrite" url="{R:0}" />
        </rule>
    </rules>
</rewrite>

ステップ2: IISのリライトモジュールを有効にする

# IIS管理コンソールを開き、サイト下にある「リライト」を選択します。
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <!-- 他の設定 -->
    </modules>
</system.webServer>

# リライトルールが正しく機能するように「runAllManagedModulesForAllRequests」属性を有効にします。

ステップ3: URLの構造を複雑にする場合

# 複数のWordPressインストールやサブディレクトリでの使用のために、ルートディレクトリを設定します。
<rule name="wordpress" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>

# サブディレクトリでのルーティングを調整します。
<system.webServer>
    <rewrite>
        <rules>
            <!-- 既存のルールに加えて -->
            <rule name="WordPress subdirectory rule (1)" stopProcessing="true">
                <match url="^subdir/(.*)" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="subdir/index.php?q={R:1}" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

ステップ4: パフォーマンスとセキュリティを考慮する

# カスタムHTTPヘッダーを通じてキャッシュ制御を行います。
<configuration>
    <system.webServer>
        <staticContent>
            <!-- 他の設定 -->
        </staticContent>
        <rewrite>
            <!-- 既存のルールに加えて -->
            <rules>
                <rule name="Prevent directory browsing" stopProcessing="true">
                    <match url="^(.+)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="false" />
                    </conditions>
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You do not have permission to access this document." />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

注意事項

  • <実践的な注意点1>: URLの書き換えはSEO最適化に重要ですが、過度な設定はパフォーマンスを悪化させる可能性があります。
  • <実践的な注意点2>: 「web.config」ファイルは他のIIS設定と相互作用するため、変更を行う前に既存の設定を理解することが重要です。
  • <セキュリティ上の注意>: 設定が適切でない場合、サイト全体や特定のページへのアクセスを許可してしまう可能性があります。常にテストを行い、必要に応じてアクセス制限を設けましょう。
  • <パフォーマンス/運用上の注意>: ルールが複雑になると処理時間が長くなるため、頻繁に変更されるような場面では適切なルール設計が必要です。

まとめ

1. <キー1>: URLのリライトはSEOとユーザビリティを改善します。

2. <キー2>: 「web.config」ファイル内の設定は正確でなければなりません。

3. <キー3>: パフォーマンスとセキュリティを考慮しながら設定を行います。

4. <キー4>: 他のIIS設定との相互作用に注意してください。

5. <キー5>: 必要なテストとモニタリングは欠かさないように。

関連記事:

お気軽にご相談ください

お見積りへ お問い合わせへ
▲