Solved

What's wrong with this web.config regex?

Posted on 2016-09-30
3
45 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 25

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

786 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