【问题标题】:IIS URL Rewrite module: redirect based on query stringIIS URL Rewrite 模块:基于查询字符串的重定向
【发布时间】:2010-11-10 19:04:37
【问题描述】:
我在根据查询字符串参数重定向到另一个 URL 时遇到一些问题。
我有地图服务的网址,例如“http://www.mydomain.com/?lon=111&lat=222&zoom=3”
我需要将其重定向到“http://www.mydomain.com/111/222/3”。
我的 web.config 中有这条规则,但它不起作用
<rule name="Location redirect">
<match url="\?lon=(\d+)&lat=(\d+)&zoom=(\d+)" />
<action type="Redirect" url="{R:1}/{R:2}/{R:3}" redirectType="Found" />
</rule>
【问题讨论】:
标签:
iis
redirect
url-rewriting
【解决方案1】:
原因是 URL 不包含查询字符串,您需要为此使用条件。如果您使用用户界面,它包含一个友好的 URL 模板,它将为您生成它。
您将在下面找到重写规则以及重定向,以便将遗留流量(丑陋的 URL)重定向到漂亮的 URL:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^lon=([^=&]+)&lat=([^=&]+)&zoom=([^=&]+)$" />
</conditions>
<action type="Redirect" url="/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="?lon={R:1}&lat={R:2}&zoom={R:3}" />
</rule>