Solved

What's wrong with this web.config regex?

Posted on 2016-09-30
3
47 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 26

Accepted Solution

by:
Dr. Klahn earned 250 total points
ID: 41823927
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
ID: 41823940
@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
ID: 41824058
Thanks, folks! Changing to .* solved the problem, including any slashes being passed along.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

790 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