-1

I am using iis 7 and trying to create a rewrite rule that will look for the following URL:

http://www.example.com/Toys/?GOOGLE/USA/Wood/trains

I'm using the following web.config which works if I omit the "?" from the URL.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Toys" stopProcessing="true">
                    <match url="^Toys/(.+)/(.+)/(.+)/(.+)" />
                    <action type="Rewrite" url="/toys/index.cfm?a={R:1}&amp;b={R:2}&amp;c={R:3}&amp;d={R:4}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

They already have the "?" hard coded in a few thousand URLS submitted to the search engines for PPC urls.

aliteralmind
  • 18,274
  • 16
  • 66
  • 102
steve
  • 1,460
  • 10
  • 17

1 Answers1

1

I'm not sure what you are trying to accomplish.

Toys/(.+)/(.+)/(.+)/(.+)

Regular expression visualization

Debuggex Demo

Eliminating the start-of-line anchor (the ^) matches your example url. With the anchor, it doesn't match, because there's text before the Toys.

Note, however, the url I put on the second line, which contains extra path elements. Your regex captures ?GOOGLE/USA/Wood/trains in the first group, because each element in your regex is .+ which is "any character, and as many as possible". Because it's greedy, it consumes these extra elements.

It would be safer to exclude slashes from each captured portion:

Toys/([^/]+)/([^/]+)/([^/]+)/([^/]+)

Regular expression visualization

Debuggex Demo

Note the difference in what it captures: Never more than four path elements, and only one element in each capture group.

An alternative (which is fine, but I prefer the slash-version above) is to make each element in your original regex reluctant:

Toys/(.+?)/(.+?)/(.+?)/(.+?)

Regular expression visualization

Debuggex Demo

  • Greedy causes that portion of the regex to match as much as possible (as long as the overall regex can still match).
  • Reluctant causes that portion to match as little as possible (as long as the overall regex can still match).

Please consider bookmarking the Stack Overflow Regular Expressions FAQ for future reference.

Community
  • 1
  • 1
aliteralmind
  • 18,274
  • 16
  • 66
  • 102
  • Thank you. Your Regex was right on the money. I used the /'s technique in the first at the stop, but I think that when coldfusion see's the ? its grabbing the whole thing. I used iis7's pattern tester in the iis manager and it shows me what I want to see but coldfusion is doing something different – steve Jul 12 '14 at 02:13