URL Rewrite - Redirect to different port and changing URL using map

☆樱花仙子☆ 提交于 2019-12-08 08:52:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!