首页 > 解决方案 > IIS URL 重写正则表达式使用选项列表定义所需的字符串块

问题描述

我正在尝试构建一个与以下内容匹配的正则表达式:

这是默认的英文网站 URL:https ://www.example.com/buy/apartment

这是默认的德国网站 URL:https ://www.example.com/de/kauf/appartement

我现在的表达方式:^([a-z]{0,2})\/(buy|kauf)\/([a-zA-Z0-9-+]+)$

所以,我尝试过的细分:

我的 IIS 重写规则:

<rule name="test rewrite">
  <match url="^([a-z]{0,2})\/(buy|kauf)\/([a-zA-Z0-9-+]+)$" />
  <action type="Rewrite" url="homes.aspx?type={R:3}&amp;language={R:1}" />
</rule>

我一直在尝试在这里测试它:https ://regex101.com/ 但我没有在这两个 URL 上得到匹配,我希望它们都匹配:https ://www.example.com/购买/公寓https://www.example.com/de/kauf/appartement

不应匹配的 URL:https ://www.example.com/en/all-objects或https://www.example.com/contact

更新 1

我现在可以使用这个表达式:https:\/\/(.*)?\/([a-z]{0,2})\/?(buy|kauf)\/([a-zA-Z0-9-+]+)$ 参见https://regex101.com/r/pieJWW/1

但是,我似乎无法将第二个 URL 中的语言参数“de”作为一个组获取。

更新 2 在 IIS web.config 中,我不使用https://www.example.com/匹配的部分,而是使用之后的部分。因此,根据@Ryszard Czech 的回答,我现在有了这个,但我无法获取 lang 参数,无论是使用{R:1}还是{R:2}

    <rule name=""test rewrite">
      <match url="^(([a-z]{0,2})\/)?(buy|kauf)\/([^\/]+)$" />
      <action type="Rewrite" url="homes.aspx?type={R:4}&amp;language={R:1}" />
    </rule>

标签: regexiisurl-rewriting

解决方案


利用

^https:\/\/([^\/]+)\/(?:([a-z]{1,2})\/)?(buy|kauf)\/([^\/]+)$

证明

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  https:                   'https:'
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^\/]+                   any character except: '\/' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (                        group and capture to \2:
--------------------------------------------------------------------------------
      [a-z]{1,2}               any character of: 'a' to 'z' (between
                               1 and 2 times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
    )                        end of \2
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    buy                      'buy'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    kauf                     'kauf'
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  (                        group and capture to \4:
--------------------------------------------------------------------------------
    [^\/]+                   any character except: '\/' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \4
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

推荐阅读