【问题标题】:URL Rewrite - Redirect to different port and changing URL using mapURL 重写 - 使用映射重定向到不同的端口并更改 URL
【发布时间】:2012-10-22 18:34:43
【问题描述】:
我想根据 HTTP_URL 重写 URL 以重定向到不同的端口,同时保留 URL 的其余部分和查询字符串(如果指定)。
例如,
http://host/john/page.aspx 应重定向到 http://host:1900/page.aspx,
http://host/paul/anotherpage.aspx?query 到 http://host:1901/anotherpage.aspx?query
和http://host/ringo 到http://host:1902/
我为每个允许的端口添加了一堆规则,但它看起来并不高效或易于管理。
我正在尝试使用地图(即 john->1900,paul->1901),但无法弄清楚如何组装所需的 URL。
有什么建议吗?
【问题讨论】:
标签:
url-rewriting
iis-7.5
【解决方案1】:
花了一些时间才能让它工作,但回头看,解决方案非常简单和优雅。
<rewrite>
<rules>
<clear />
<rule name="Redirect known names to ports" stopProcessing="true">
<match url=".*" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/(.*?)/(.*)" />
<add input="{NameToPort:{C:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}:{C:3}/{C:2}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="NameToPort">
<add key="john" value="1900" />
<add key="paul" value="1901" />
<add key="ringo" value="1902" />
</rewriteMap>
</rewriteMaps>
</rewrite>
如果这就是您要找的,请告诉我。