Solved

What's wrong with this web.config regex?

Posted on 2016-09-30
3
34 Views
Last Modified: 2016-09-30
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
Comment
Question by:SiobhanElara
3 Comments
 
LVL 23

Accepted Solution

by:
Dr. Klahn earned 250 total points
Comment Utility
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
 
LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 250 total points
Comment Utility
@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
 

Author Closing Comment

by:SiobhanElara
Comment Utility
Thanks, folks! Changing to .* solved the problem, including any slashes being passed along.
0

Featured Post

Want to promote your upcoming event?

Attending an event? Speaking at a conference? Or exhibiting at a tradeshow? Easily inform your contacts by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

Join & Write a Comment

I had a question today where the user wanted to know how to delete an SSL Certificate, so I thought that I would quickly add this How to! Article for your reference. WHY WOULD YOU WANT TO DELETE A CERTIFICATE? 1. If an incorrect certificate was …
You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now