IIS:使用正则表达式重写URL,但保留查询字符串 [英] IIS: Rewrite URL with regex but keep query strings

查看:78
本文介绍了IIS:使用正则表达式重写URL,但保留查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下链接

https://example.com/myapp/green?&lang=zh-CN&instance=some%20instance

我需要将其重写为以下链接

I need to rewrite it to the following link

https://example.com/myapp?color=green&lang=zh-CN&instance=some%20instance

链接中的颜色可以是任何颜色,但需要像第二个链接中那样进行重写,以便将尾部斜杠替换为?,后跟单词 color = 和颜色词末尾的?需要删除.

The color in the link can be any color but it needs to be rewritten like in the 2nd link so that the trailing slash is replaced with a ? followed by the word color= and the ? at the end of the color word needs to be removed.

/myapp/green?变为/myapp?color = green

/myapp/blue?变为/myapp?color = blue

依此类推,同时保持其余查询字符串& lang = en& instance = some%20instance 完整

and so forth, all while keeping the rest of the query string &lang=en&instance=some%20instance intact

我已经尝试过用这种方法来重新表达自己的方式,但是我通常会捕获所有内容,或者无意间忽略了其余的查询字符串.

I've tried regexing my way out of this but I usually catch everything or unintentionally omit the rest of the query string.

关于什么是最佳方法的任何想法?

Any ideas on what's the best approach?

注意到IIS,当应用于应用程序级别(而非网站级别)时,输入URL路径位于'/myapp/'之后,我需要删除该斜杠.这是否意味着我必须将其应用于网站级别?

noticed that IIS, when applying to application level (not website level), the input URL path is after '/myapp/' and I need that trailing slash removed. Does this mean I'll have to apply it to the website level?

推荐答案

您可以使用以下url重写规则(在网站级别添加此规则):

You could use below url rewrite rule(add this rule at site level):

<rule name="color query" enabled="true" stopProcessing="true">
                <match url="myapp/(.*)/$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{QUERY_STRING}" pattern="lang=(.*)&amp;instance=(.*)" />
                </conditions>
                <action type="Redirect" url="http://www.sample1.com/myapp?color={R:1}&amp;lang={C:1}&amp;instance={C:2}" appendQueryString="false" />
            </rule>

注意:您无法从URL中删除myapp/,它将自动添加到URL中.

Note: you could not remove the myapp/ from URL it will be added automatically in URL.

这篇关于IIS:使用正则表达式重写URL,但保留查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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