Reputation: 28763
I want to setup a redirect in IIS that redirects all multi-level URLs on my site that end in /blog
to a root-level subdirectory called /blog
. For example, https://www.example.com/sub1/sub2/blog
would redirect to https://www.example.com/blog
.
I wrote the following regex pattern:
blog/?$
The problem is that while this does match https://www.example.com/sub1/sub2/blog
, it also matches https://www.example.com/blog
, which is itself the URL I'm redirecting to. Therefore, hitting /sub1/sub2/blog
redirects to /blog
, which itself matches and redirects to /blog
, etc., causing it to get stuck in a loop redirecting to itself.
How do I write the regex pattern so that a multi-level URL that ends in /blog
will redirect to the root-level /blog
page, without the root-level page itself matching?
Upvotes: 0
Views: 1485
Reputation: 3494
Please try this
<rule name="rewrite rule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/(.+)/blog/?$" />
</conditions>
<action type="Redirect" url="https://www.example.com/blog" redirectType="Temporary" />
</rule>
Or you can whitlist
<rule name="rewrite rule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="blog/?$" />
<add input="{URL}" pattern="^/blog/?$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/blog" redirectType="Temporary" />
</rule>
Upvotes: 1