Error rewriting rewritten URL in IIS Rewrite 2 URL

I am trying to create a system for working with images and their modified versions from GridFS using MVC3 and IIS URL Rewrite 2. After testing, I realized that serving images directly from the file system is 10 times faster than serving them using a GridFS file stream. Then I decided to save the originals in GridFS and create a copy of the source file and resize the versions on the local server file system using a combination of Url Rewrite 2 and Asp.Net handlers.

Here are the rewriting rules that I use to service the original and modified versions:

<rule name="Serve Resized Image" stopProcessing="true"> <match url="images/([az]+)/[a-f0-9]+/[a-f0-9]+/[a-f0-9]+/([a-f0-9]+)-([a-f0-9]+)-([a-f0-9]+)-([0-9]+)\.(.+)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> </conditions> <action type="Rewrite" url="/Handlers/ImageResizer.ashx?Uri={REQUEST_URI}&amp;Type={R:1}&amp;Id={R:2}&amp;Width={R:3}&amp;Height={R:4}&amp;ResizeType={R:5}&amp;Extension={R:6}" appendQueryString="false" logRewrittenUrl="true" /> </rule> <rule name="Serve Original Image" stopProcessing="true"> <match url="images/([az]+)/[a-f0-9]+/[a-f0-9]+/[a-f0-9]+/([a-f0-9]+)\.(.+)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> </conditions> <action type="Rewrite" url="/Handlers/Images.ashx?Uri={REQUEST_URI}&amp;Type={R:1}&amp;Id={R:2}&amp;Extension={R:3}" appendQueryString="false" logRewrittenUrl="true" /> </rule> 

As you can see, overwrite the engine check if the file exists in the file system, and if not. rewrites the URL and sends the request to the handler. The handler serves the stream and writes the file to the file system. At the next request, the file is submitted directly from the file system. I divided the files into folders, dividing them with 24 char ID (MongoDB object identifier as a string) to avoid getting thousands of images in one folder.

Here is an example of the original image request:

http://localhost/images/test/50115c53/1f37e409/4c7ab27d/50115c531f37e4094c7ab27d.jpg

This and modified versions work without problems.

Since this URL is too long and has duplicates in it, I decided to use the rewrite mechanism again to shorten the URL to automatically create folder names. Here is the rule that I put on top:

 <rule name="Short Path for Images"> <match url="images/([az]+)/([a-f0-9]{8})([a-f0-9]{8})([a-f0-9]{8})(.+)" /> <action type="Rewrite" url="images/{R:1}/{R:2}/{R:3}/{R:4}/{R:2}{R:3}{R:4}{R:5}" appendQueryString="false" logRewrittenUrl="true"></action> </rule> 

When I request an image using this rule, for example, with the following URL:

http://localhost/images/test/50115c531f37e4094c7ab27d.jpg

it serves only for the image, if the image is already in the file system, otherwise I get the following error:

HTTP Error 500.50 - Error rewriting the url. The page cannot be displayed because an internal server error has occurred.

I checked the IIS log file entry for the request. It does not show any details except:

2012-08-02 14:44:51 127.0.0.1 GET /images/test/50115c531f37e4094c7ab27d.jpg - 80 - 127.0.0.1 Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/537.1+(KHTML,+like+Gecko)+Chrome/21.0.1180.60+Safari/537.1 500 50 161 37

Successful requests, on the other hand, register a rewritten URL, for example:

GET /Handlers/ImageResizer.ashx Uri=/images/test/50115c53/1f37e409/4c7ab27d/50115c531f37e4094c7ab27d-1f4-1f4-2.jpg&Type=test&Id=50115c531f37e4094c7ab27d&Width=1f4&Height=1f4&ResizeType=2&Extension=jpg

Elmah and EventLog also show nothing. A file system logger has been added to the top of my controller method and does not log these specific problematic requests.

Can anyone suggest a workaround to make it work?

Edit: after RuslanY the suggestion about Failed to trace the request , I was able to determine the error:

 ModuleName: RewriteModule Notification: 1 HttpStatus: 500 HttpReason: URL Rewrite Module Error. HttpSubStatus: 50 ErrorCode: 2147942561 ConfigExceptionInfo: Notification: BEGIN_REQUEST ErrorCode: The specified path is invalid. (0x800700a1) 

The result of the entire trace can be seen here (IE only)

Unfortunately, this still does not lead me to a solution, because the second rule (therefore, the reduction rule) works when the file exists in the file system.

+4
source share
1 answer

As an alternative approach to using UrlRewrite to perform this check, why not use disk-based caching for application requests. Dynamic images will be generated, and the ARR caching infrastructure will save the generated images to disk. Less clutter and I used ARR for great success in production scenarios. The disk cache is maintained between IIS restarts and can live as long as you talk (by default, the cache information from the response is used, but you can override this to make it longer).

Application Request Routing

+2
source

All Articles