The first rewrite rule (below), “Rewrite to readable URL”, works perfectly. The second rule, “Remove WWW prefix”, removes the WWW prefix from the URL, so this:
http://www.mydomain.com/blog...
Becomes this:
http://mydomain.com/blob...
Now this is causing a little havoc. Although the article loads fine, if the rule has to work, for example WWW is present, the URL in the browser address bar unfortunately changes back to the unreadable version like this:
http://mydomain.com/blog/article.asp?id=1&title=blog-title
But the rewrite has worked, the WWW has been removed. But why has it changed from the friendly URL to the non-friendly URL? Can anybody spot my mistake or suggest how to rectify this?
This is part of my web.config file:
<rules>
<rule name="Rewrite to readable URL">
<match url="^blog/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="blog/article.asp?id={R:1}&title={R:2}" />
</rule>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com" />
</conditions>
<action type="Redirect" url="https://mydomain.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
It looks like you feed the result of your rewrite process into the redirect. Try changing the order of the two rules.
For a little more control, you can also add stopProcessing=”true” to a rule tag.
In your case, the rewriting and redirecting should be completely separate processes.