asp.neturlrewriting.net

How to redirect images requests to another folder using urlrewriting.net


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


Solution

  • 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>