简体   繁体   中英

Rewrite Map Rule to match any URL extension

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.

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.

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

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.

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

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

    <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. Changed {Test:{REQUEST_URI}} to {Test:{R:1}} so it uses the matched input from the match url (.*)
  3. Take out leading / from rewrite map keys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM