重写映射规则以匹配任何URL扩展

时间:2021-03-28 18:05:36

I'm fairly new to rewrite maps, but we did get ours to work on a very basic level. After a website redesign, we set up an extensive rewrite map (thousands o rules) to point the old pages to the new ones. The trouble we're having is that we're having to add multiple values for the same page in order for the rewrite to work.

我是一个相当新的重写地图,但我们确实让我们的工作在一个非常基础的水平上。在网站重新设计之后,我们设置了一个广泛的重写映射(数千条规则)来将旧页面指向新页面。我们遇到的麻烦是我们必须为同一页面添加多个值才能使重写工作。

Example:

http://www.abc123.com/About  -->  http://www.abc123.com/about-us
http://www.abc123.com/About.aspx  -->  http://www.abc123.com/about-us
http://www.abc123.com/about/  -->  http://www.abc123.com/about-us
http://www.abc123.com/about.aspx  -->  http://www.abc123.com/about-us

There should be a way to wildcard anything after the base URL in the regular expression - I'm expecting something like this: ^./[about]$ which would be great if ALL urls contained "about" but they don't.

应该有一种方法可以在正则表达式中的基本URL之后通配任何东西 - 我期待这样的东西:^。/ [about] $如果所有URL都包含“about”但是它们没有,这将是很好的。

Also note that we aren't redirecting by directory, but rather by file name. It's that our CMS is set up not to use the .aspx extension, so any extension will work.

另请注意,我们不是按目录重定向,而是按文件名重定向。我们的CMS设置为不使用.aspx扩展名,因此任何扩展都可以使用。

What I want is to only have to have ONE rule for each URL that looks like: "http://www.abc123.com/about" and it will point all of the above variations to the new URL regardless if it does not have an extension or if the extension is .html, .asp, .aspx, or .whatever

我想要的只是每个URL看起来像一个规则:“http://www.abc123.com/about”,它会将所有上述变化指向新的URL,无论它是否没有扩展名或扩展名为.html,.asp,.aspx或.whatever

Is that beyond the capabilities of the rewrite rules or is there some basic regular expression I am missing?

这是否超出了重写规则的能力,还是我缺少一些基本的正则表达式?

Here is the rule we are using:

以下是我们使用的规则:

<rule name="Redirect Rule for Legacy Redirects" enabled="true" stopProcessing="true">
   <match url=".*" />
   <conditions>
        <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
  </conditions>
  <action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>

Any insight would be much appreciated.

任何见解都会非常感激。

2 个解决方案

#1


0  

[Hh][Tt][Tt][Pp]://(([^/])/)[Aa][Bb][Oo][Uu][Tt].*

See https://regex101.com/r/rZhJyz/1, just append "about-us" to the Group #1 match.

请参阅https://regex101.com/r/rZhJyz/1,只需将“about-us”附加到第1组比赛即可。

#2


0  

Had to figure this out today. It can be done by using the <match url> regex to strip off the extension, and then using the matched portion from here as the input for the rewrite map lookup.

今天必须弄清楚这一点。可以通过使用 正则表达式去除扩展,然后使用此处的匹配部分作为重写映射查找的输入来完成。

The rewrite map keys must NOT have a starting /.

重写映射键不能有一个起始/。

The rule (and sample map) looks like this, example for stripping off .aspx extension (could be generalised):

规则(和示例映射)看起来像这样,剥离.aspx扩展的示例(可以推广):

    <rewrite>
        <rewriteMaps>
            <rewriteMap name="Test">
                <add key="test" value="http://www.google.com" />
                <add key="test.aspx" value="http://www.google.com" />
            </rewriteMap>
        </rewriteMaps>
        <rules>
            <rule name="Rewrite Map Optional Aspx Extension" stopProcessing="true">
                <match url="^(.*?)(\.aspx)?$" />
                <conditions>
                    <add input="{Test:{R:1}}" pattern="(.+)" />
                </conditions>
                <action type="Redirect" url="{C:1}" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>

The important changes from a standard rewrite map rule are:

标准重写映射规则的重要更改是:

  1. Adding (\.aspx)? as an optional part of the match url, and have have added ? to .* to make the initial .* not greedy so it doesn't include the extension.
  2. 添加(\ .aspx)?作为匹配网址的可选部分,并已添加? to。*使初始。*不贪心所以它不包括扩展名。

  3. Changed {Test:{REQUEST_URI}} to {Test:{R:1}} so it uses the matched input from the match url (.*)
  4. 将{Test:{REQUEST_URI}}更改为{Test:{R:1}},以便它使用匹配网址(。*)中的匹配输入

  5. Take out leading / from rewrite map keys
  6. 取出前导/重写地图键

#1


0  

[Hh][Tt][Tt][Pp]://(([^/])/)[Aa][Bb][Oo][Uu][Tt].*

See https://regex101.com/r/rZhJyz/1, just append "about-us" to the Group #1 match.

请参阅https://regex101.com/r/rZhJyz/1,只需将“about-us”附加到第1组比赛即可。

#2


0  

Had to figure this out today. It can be done by using the <match url> regex to strip off the extension, and then using the matched portion from here as the input for the rewrite map lookup.

今天必须弄清楚这一点。可以通过使用 正则表达式去除扩展,然后使用此处的匹配部分作为重写映射查找的输入来完成。

The rewrite map keys must NOT have a starting /.

重写映射键不能有一个起始/。

The rule (and sample map) looks like this, example for stripping off .aspx extension (could be generalised):

规则(和示例映射)看起来像这样,剥离.aspx扩展的示例(可以推广):

    <rewrite>
        <rewriteMaps>
            <rewriteMap name="Test">
                <add key="test" value="http://www.google.com" />
                <add key="test.aspx" value="http://www.google.com" />
            </rewriteMap>
        </rewriteMaps>
        <rules>
            <rule name="Rewrite Map Optional Aspx Extension" stopProcessing="true">
                <match url="^(.*?)(\.aspx)?$" />
                <conditions>
                    <add input="{Test:{R:1}}" pattern="(.+)" />
                </conditions>
                <action type="Redirect" url="{C:1}" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>

The important changes from a standard rewrite map rule are:

标准重写映射规则的重要更改是:

  1. Adding (\.aspx)? as an optional part of the match url, and have have added ? to .* to make the initial .* not greedy so it doesn't include the extension.
  2. 添加(\ .aspx)?作为匹配网址的可选部分,并已添加? to。*使初始。*不贪心所以它不包括扩展名。

  3. Changed {Test:{REQUEST_URI}} to {Test:{R:1}} so it uses the matched input from the match url (.*)
  4. 将{Test:{REQUEST_URI}}更改为{Test:{R:1}},以便它使用匹配网址(。*)中的匹配输入

  5. Take out leading / from rewrite map keys
  6. 取出前导/重写地图键