问题
I want to rewrite URL to redirect to different port, based on HTTP_URL while preserving rest of URL and query string (if specified).
For example,http://host/john/page.aspx
should be redirected to http://host:1900/page.aspx,
http://host/paul/anotherpage.aspx?query
to http://host:1901/anotherpage.aspx?query
and http://host/ringo
to http://host:1902/
I've added bunch of rules for every allowed port, but it does not look efficient or manageable.
I'm trying to employ map, (ie john->1900, paul->1901) but cannot figure out how to assemble desired URL.
Any suggestions?
回答1:
It took some fiddling to get it working but looking back at it the solutions is quite simple and elegant.
<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>
Let me know if this is what you were looking for.
来源:https://stackoverflow.com/questions/13189505/url-rewrite-redirect-to-different-port-and-changing-url-using-map