I'm running IIS 10 on Windows Server 2019, with a WordPress install on it (among other things), with URL Rewrite 2.1 installed. IIS is configured properly for the Wordpress app, which is working fine in of itself.
I'm trying to define URL rewrite rules from the server's root directory that will send certain requests to the WordPress application.
If I browse to https://this.is.my.server.com/wp/this-is-a-test/
, the page loads fine.
I define a rewrite map that includes an entry to send /this-is-a-test
to /wp/this-is-a-test/
, and create a rewrite rule that uses this map to redirect requests, it works fine - the original URL returns a 301 HTTP status, and the request gets forwarded along to the expected URL. Here is the rule:
<rule name="Rewrite rule1 for RewriteFriendlyUrlsToBlog" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RewriteFriendlyUrls:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
However, I need a rewrite rule, not a redirect. If I edit this rule to be a rewrite instead, I get a 404 error. The adjusted rule is exactly the same as the previous, except for being a rewrite instead of a redirect:
<rule name="Rewrite rule1 for RewriteFriendlyUrlsToBlog" enabled="true" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{RewriteFriendlyUrls:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
The detailed error information includes:
Requested URL: https://this.is.my.server.com/wp/this-is-a-test/
...which suggests two things to me:
index.php
is not executing for some reason.But I don't know what would need to be done for this rewrite rule to work properly.
Through trial-and-error, I've found that to get a rewrite going properly, I need to rewrite to index.php
- ensuring 'Append query string' option is checked - and not the expected URL.
This seems to work:
<rule name="TEST single URL rewrite to PHP" enabled="false" patternSyntax="ECMAScript" stopProcessing="false">
<match url="^this-is-a-test" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="/wp/index.php" />
</rule>
Also works using a rewrite map.
It suggests to me that, for the rewrite, the rule within the WordPress folder that rewrites requests that are neither files nor folders to index.php isn't being processed, regardless what your 'stopProcessing' option is set to.