正则表达式从URL中提取部分并将其用作url重写中的引用 [英] Regex to extract a part out of a URL and use it as a reference in url rewrite

查看:167
本文介绍了正则表达式从URL中提取部分并将其用作url重写中的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

基本上,我想要做的是使用IIS urlrewrite模块在用户访问另一个站点上的URL时重定向到特定的URL。唯一的问题是我必须捕获一些查询字符串,并将其移动到重定向中。我想我实际上有正确的正则表达式,但它正在我的web.config中实现它导致问题。

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. I think I actually DO have the regex correct for this, but it's implementing it in my web.config that is causing the problem.

所以这里是输入,URL用户可以访问的内容如下所示:

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

我希望匹配该URL(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&(language=(..))" ignoreCase="true" />
          <action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

这对我来说似乎都是正确的。但是,IIS正在喋喋不休地说我的web.config无效。具体来说,它说实体('未定义'并且它指向(在语言之前作为问题。因此,在我修复之前,我无法构建或部署此应用程序。如果我没有构建它,那么只是尝试将其放入web.config,我在加载网站时出错:

this all seems right to me. however, IIS is balking and saying that my web.config is invalid. specifically, it says "Entity '(' is not defined" and it's pointing to the ( right before language as the problem. So, I can't build or deploy this application till I fix that. If i build it without and then just try to drop that into the web.config, i get an error loading the site:

HTTP错误500.19 - 内部服务器错误
无法访问请求的页面因为页面的相关配置数据无效。

HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.

有人可以帮我弄清楚如何正确地将捕获组置于重写规则中吗?

Can someone help me figure out how to put a capture group into a rewrite rule properly?

推荐答案

您忘记编码&符号。应该是:

You forgot to code the ampersand. Should be:

<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>

这篇关于正则表达式从URL中提取部分并将其用作url重写中的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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