Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 69
  • Last Modified:

What's wrong with this web.config regex?

I have a web.config file with the following rule in it:

<rule name="redirectStaticFileRequestsToDownloadHandler" stopProcessing="true">
       <match url="board\/([0-9]+)\/boarddocs\/([^/]+)?$" />
      <action type="Redirect" url="/download?filename={R:2}" appendQueryString="false" />
</rule>

Open in new window


If anyone tries to access a file from a URL like "board/[any number]/boarddocs/[any string]" it will redirect them to "/download?filename=[that second string from the URL example]". I've run my code through http://www.regexr.com/ and it says it should be capturing group #2. I tested replacing {R:2} with {R:1} and it redirects to the first captured string ([any number]) like expected.

What am I doing wrong in trying to reference that second string? Thanks!
0
SiobhanElara
Asked:
SiobhanElara
2 Solutions
 
Dr. KlahnPrincipal Software EngineerCommented:
The up-caret ^ is a reserved character in regexes which indicates "start of string".  Since the location of the second regex is well past the start of string, there will be no match on any input.

Suggest:  .* instead which will capture anything to the end of the original URI.
0
 
Russ SuterCommented:
@Dr. Klahn
The caret here is specifying that the capture group matches against any character that is not a '/' character. This is perfectly acceptable Regex syntax.

@SiobhanElara
This regex won't match if the user has additional '/' characters after. For example, the following URL would match:
http://board/2341/boarddocs/foo.html
But this won't:
http://board/2341/boarddocs/foo/mydoc.html

Could that be your issue here?
1
 
SiobhanElaraAuthor Commented:
Thanks, folks! Changing to .* solved the problem, including any slashes being passed along.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now