正则表达式使用url重写重定向URL [英] Regex to redirect a url using url rewrite

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

问题描述

好的,我想知道是否有人可以借助我想写的正则表达式。

OK, I'm wondering if someone can lend a hand with a regex I'm trying to write.

基本上,我想要做的是使用IIS urlrewrite模块在用户访问另一个站点上的URL时重定向到特定的URL。唯一的问题是我必须捕获一些查询字符串,并将其移动到重定向。

所以这里是输入,用户可以访问的URL看起来像:

Basically, what I want to do is use IIS urlrewrite module to do a redirect to a specific URL if the user accesses a URL on another site. The only catch is I have to also capture a bit of the query string, and move it into the redirect.
so here is the input, the URL that a user may access would look like:

https: //of.example.com/sfsv3.aspx?waform=pro&language=en

https://of.example.com/sfsv3.aspx?waform=pro&language=en

我想匹配该网址(或者http或https,不区分大小写),并从中捕获一条信息,即双字母语言代码。那么网址我想转发用户看起来像:

I want to match that URL (either http or https, case insensitive), and capture from it also one piece of information, the two letter language code. then the url i want to forward the user to looks like:

http://example.com/ca/en/ppf

http://example.com/ca/en/ppf

(其中en被替换为任何我上面捕获了)

(where en is replaced by whatever i captured above)

所以,我正在使用IIS Rewrite模块,我已经获得了我的输入数据和正则表达式,到目前为止我的正则表达式模式是这个:

So, I'm working with IIS Rewrite module, and I've gotten my input data and regex in, so far the regex pattern I have is this:

https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))

所以基本上我匹配整个字符串,加上一个组和一个语言的子组及其代码。在IIS测试模式对话框中,这是有效的。

so basically i'm matching the whole string, plus a group and a subgroup for language and it's code. in the IIS test pattern dialog, this is working.

我得到以下内容

{R:1} language = en

{R:1} language=en

{R:2} en

太棒了!所以我的IIS重写规则应该像这样重定向用户:

great! so then my IIS rewrite rule should look like this to redirect the user:

<system.webServer>
    <rewrite>
      <rules>
        <rule name="test" stopProcessing="true" enabled="true">
          <match url="https?://of.example.com/sfsv3.aspx\?waform=pro&amp;(language=(..))" ignoreCase="true" />
          <action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

这对我来说似乎都是正确的。但是,重定向没有发生。它似乎与部件有问题\? (用于标记查询字符串开头的转义问号)。如果这包括,那么重定向就不会发生。

this all seems right to me. however, the redirect is not occurring. it seems to have a problem with the part \? (an escaped question mark to mark the start of the query string). if this is included, then the redirect simply does not happen.

你能帮我弄清楚如何让它运作吗?

Can you help me figure out how to get it work?

推荐答案

为了记录,我想通了。这是正则表达式的一个特例,在web.config中运行它作为urlrewrite操作的一部分。在这种情况下,您无法使用简单的正则表达式处理查询字符串,您必须在查询字符串上添加条件。这是最终工作的结果:

for the record, I figured it out. this is a special case of regex, running it inside a web.config as part of a urlrewrite action. in that case, you can't handle the query string with simple regex, you have to put in conditions on the query string. Here's what eventually ended up working:

    <system.webServer>
        <rewrite>
            <rules>
                <rule name="redirect" stopProcessing="true" stopProcessing="true" enabled="true">
                    <match url="sfsv3.aspx\.aspx" ignoreCase="true"/>
                    <conditions>
                        <add input="{QUERY_STRING}" pattern="subject=PROPRCH" />
                        <add input="{QUERY_STRING}" pattern="(..)/subject" />
                    </conditions>
                    <action type="Redirect" url="https://www.example.com/ca/{C:1}/forms/ppf" appendQueryString="false"/>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>

这篇关于正则表达式使用url重写重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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