[Azure][Node.js][WordPress] Node.jsとWordPressを同一サイトに共存させるリライト方法

今回は Azure に Node.js+Express で稼働している Web サイトに WordPress を載せる案件で試行錯誤した結果を記録しておきます。

サイト構成は以下のとおりです。

D:\wwwroot\                  Node.js+Express 環境
D:\wwwroot\wordpress\ WordPress 環境

一見簡単そうですが、Node.js 環境は Rewrite をメインに使っており、wordpress フォルダをどのように評価してくれるかが難しかったです。

wordpress フォルダは仮想で割り振ることもできるかもしれませんが、下記の IsFile IsDirectory のパスを調整する必要があります。これらはファイルやディレクトリが実際に存在するかどうかを判定するものです。

なお、wordpress フォルダに web.config を置いても参照されませんでした。ルートの web.config に WordPress 関係を記述しなければならないようです。

そこで、ルートの Rewrite 設定をこのようにしてみました。

<rule name="Redirect WordPress Resources" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="^(wp-.+)$"/>
    <conditions>
        <add input="{APPL_PHYSICAL_PATH}wordpress/{R:1}" matchType="IsFile" negate="false"/>
    </conditions>
    <action type="Redirect" url="/wordpress/{R:1}" redirectType="Found"/>
</rule>
<rule name="Redirect WordPress" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="^(wp-.+)"/>
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
    </conditions>
    <action type="Redirect" url="wordpress/{R:1}" redirectType="Found"/>
</rule>
<rule name="Handle WordPress Static Resources" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="^wordpress\/.*\.(css|js|jpe?g|png|ico|gif|php)$"/>
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="false"/>
    </conditions>
    <action type="None"/>
</rule>
<rule name="WordPress Core" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="^wordpress\/(.*)\/"/>
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="false"/>
    </conditions>
    <action type="Rewrite" url="wordpress/{R:1}/index.php"/>
</rule>
<rule name="WordPress Core Root" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="^media\/"/>
    <action type="Rewrite" url="wordpress/index.php"/>
</rule>

最初の"Redirect WordPress Resources"は、wp- からはじまるファイル指定のときに、wordpress フォルダを付けてリダイレクトさせます。リダイレクトするときに IsFile で、ファイルが存在するかどうかを判定しています。このとき、環境変数{APPL_PHYSICAL_PATH}でディレクトリ"D:\wwwroot\"を取得しています。

次の"Redirect WordPress"で、ディレクトリのリダイレクトをしています。negate=true で、ファイルでもディレクトでもないとしています。

"Handle WordPress Static Resources"は拡張子指定で、存在するファイルは静的ファイルとみなして何もしないよう設定しています。

"WordPress Core", "WordPress Core Root"は、wordpress 以下のディレクトリにある index.php を呼び出す設定です。IsFile も設定すればより固いでしょう。