URL重写-重定向到其他端口并使用map更改URL [英] URL Rewrite - Redirect to different port and changing URL using map

查看:424
本文介绍了URL重写-重定向到其他端口并使用map更改URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写URL以基于HTTP_URL重定向到其他端口,同时保留URL的其余部分和查询字符串(如果指定). 例如,
http://host/john/page.aspx应该重定向到http://host:1900/page.aspx,
http://host/paul/anotherpage.aspx?queryhttp://host:1901/anotherpage.aspx?query
http://host/ringohttp://host:1902/
我为每个允许的端口添加了一堆规则,但是看起来效率不高或无法管理.
我正在尝试使用地图(例如john-> 1900,paul-> 1901),但无法弄清楚如何组装所需的URL.
有什么建议吗?

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?

推荐答案

花了一些时间才使它工作,但回头看,解决方案非常简单而优雅.

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.

这篇关于URL重写-重定向到其他端口并使用map更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆