2

I'm using urlrewriting.net to redirect a cascading stylesheet image request. I've been trying without luck to do so. Here's the rewrite I added:

<add name="reportImagesRedirect"
                    virtualUrl="^~/estadisticas/(.*).png"
                    rewriteUrlParameter="ExcludeFromClientQueryString"
                    destinationUrl="~/images/$1"
                    ignoreCase="true" />

I'd like to know if there's something wrong with it. I'm trying to link all the http get requests made to one folder to be redirected to the images folder. So for instance when I make a request like this

http://localhost:8080/estadisticas/spines.png

I want the web server to look the image in

http://localhost:8080/images/spines.png

Raúl Roa
  • 12,061
  • 13
  • 49
  • 64
  • virtualUrl="^~/estadisticas/(.+).png$" --- notice the Regex starts with ^ and also ends with $. This will contain requests so that if you get "~/estadicticas/somthing.png.aspx", it wont try to map to an image. Not that it will ever happen, it's just good practice – Chase Florell Jan 24 '10 at 05:55

2 Answers2

1

You need to flip it.

<add name="reportImagesRedirect"
                destinationUrl="~/images/$1.png"
                rewriteUrlParameter="ExcludeFromClientQueryString"
                virtualUrl="^~/estadisticas/(.+).png$"
                ignoreCase="true" />

Here is a little article I wrote on using UrlRewriting.Net for extensionless URL's.

EDIT: I changed the parameters a bit. if you are keeping the extensions, you need to have .png at the end of both the virtual and the destination

EDIT: You might also need to make the following modification to the system.webServer tag

<system.webServer>
     <modules runAllManagedModulesForAllRequests="true">
     </modules>
</system.webServer>
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • Um... the order of the attributes makes absolutely NO difference. – Chris Jan 24 '10 at 05:34
  • I edited my question with an example of the result I'm expecting in case it wasn't clear enough. – Raúl Roa Jan 24 '10 at 05:35
  • Why would you down vote my answer. I didn't "JUST" rearrange the order of the attributes. I changed the way the VirtualUrl and the destinationUrl's were written. The VirtualUrl needs the Regex, while the DestinationUrl needs to be where the Actual file is located... it must contain the extension as well. – Chase Florell Jan 24 '10 at 05:44
  • Ok, there's a problem with the regex... since the request it's being made to ...\estadisticas\^\images\spines.png, instead of ...\images\spines.png. I fixed it removing ^ – Raúl Roa Jan 24 '10 at 05:50
  • Yup, you're right Raul. I didn't notice that bit since I was just modifying his xml. I'll edit now. – Chase Florell Jan 24 '10 at 05:52
  • A) I didn't downvote Rock and B) flagging is not really for that. You both could use a little maturity when partaking in community discussions such as S/O – Chris Jan 24 '10 at 06:08
  • Sorry Chris. I didn't flag or down vote anyone, Just bitched a little when my answer was down voted when it wasn't wrong. – Chase Florell Jan 24 '10 at 14:33
0

You may need to set up a wildcard mapping. IIS will not forward requests for anything but ASP.NET files (.aspx, .asmx, .axd, .ashx, etc...) to the ASP.NET handler by default. Since your rewriter runs in the ASP.NET handler, the request for the image will never reach it. Adding a wildcard mapping forces IIS to let ASP.NET (and consequently, the rewrite handler) handle all requests, including images.

Chris
  • 27,596
  • 25
  • 124
  • 225
  • I don't recommend wildcard mappings, it's just not needed – Chase Florell Jan 24 '10 at 05:49
  • also IIS7 and .NET 3.5 allow you to do this [runAllManagedModulesForAllRequests="true"] from within your web.config and alleviates the need for Wildcard Mapping all together. – Chase Florell Jan 24 '10 at 06:02
  • @Rock: it absolutely IS needed if you're running IIS 6 or IIS 7 in classic mode. I never assume any particular version. Also, lose the whiny attitude. I see that you downvoted my answer because you think I downvoted yours, which is not the case. Not very mature, IMO. – Chris Jan 24 '10 at 06:06