问题
I have had a website with /contact.aspx
which gets a succesful redirect to /contact
But I want to redirect my old /content.aspx?contentid=123
to /about/123
Here is my redirect XML part in my web.config:
<rewrite>
<rules>
<rule name="contact" patternSyntax="Wildcard" stopProcessing="true">
<match url="contact.aspx" />
<action type="Redirect" url="Contact" redirectType="Found" />
</rule>
<rule name="Content" patternSyntax="Wildcard" stopProcessing="true">
<match url="content.aspx?contentID=*" />
<action type="Redirect" url="About/{R:1}" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
</rewrite>
Visiting mydomain.com/content.aspx?contentid=123 gives me a 404.
also tried without appendQueryString="false"
Seems easy to fix to me, but I am missing something...
using a regex instead of wildcard also gives a 404:
<rule name="Content">
<match url="content.aspx?contentID=([0-9]+)" />
<action type="Redirect" url="About/{R:1}" redirectType="Found" />
</rule>
回答1:
From IIS rewrite module configuration documentation.
The URL string that is evaluated against the pattern does not include the query string. To include the query string in the rule evaluation you can use the QUERY_STRING server variable in the rule’s condition.
So after a big of test jiggery-pokery, I think this should work:
<rule name="Content" patternSyntax="ECMAScript" stopProcessing="true">
<match url="content.aspx" />
<conditions>
<add input="{QUERY_STRING}" pattern="contentID=([^&]+)" />
</conditions>
<action type="Redirect" url="About/{C:1}" redirectType="Found" appendQueryString="False" />
</rule>
来源:https://stackoverflow.com/questions/31589259/iis7-url-rewrite-for-webforms-aspx-to-mvc-page