0
votes

I am working with an mvc4 applciation running in IIS 7.5 and I am having trouble with rewrite rules I have set up. The following is my section from the web.config.

 <rewrite>
        <rules>
            <rule name="RewriteImage">
                <match url="/myassets/([_0-9a-z-]+)/images/category/([_0-9]+)-([_0-9a-z-]+)-([0-9]+)" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Rewrite" url="/myassets/{R:1}/images/category/cat_{R:2}_{R:4}_{R:3}.jpg" />
            </rule>
        </rules>
    </rewrite>

What I want to do in above url rewrite images to another path.

I supply the following url https://localhost/myassets/en-uk/images/category/123456-5x10-1_my+image+description.jpg and expect it to rewrite to https://localhost/myassets/en-uk/images/category/cat_123456_1_5x10.jpg but it doesn't.

When I test it in IIS, all works fine, but via the browser the rule never kicks in. I am looking for suggestions as to where I am gone wrong as this is becoming very frustrating!

This is the only rule I have, I tried a simple redirect rule which redirects uppercase url's to lower case urls and this works fine.

One thing I did notice in my web.config, the first rewrite tag has a blue squiggly under saying it its not recognised section of system.webserver.

Application is running in integrated mode.

1
How do you know the rule never kicks in? Does the file https://localhost/myassets/en-uk/images/category/cat_123456_1_5x10.jpg exist?cheesemacfly
What if you remove <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />?cheesemacfly
Makes no difference if I doamateur
So the picture https://localhost/myassets/en-uk/images/category/cat_123456_1_5x10.jpg exists and when you call https://localhost/myassets/en-uk/images/category/123456-5x10-1_my+image+description.jpg it doesn't display it, right? What do see instead? What if you use the Failed Request Tracing tool?cheesemacfly

1 Answers

0
votes

Problem is that your regexp wasn't valid. Note from official documentation:

For example, if a request was made for this URL: http://www.example.com/content/default.aspx?tabid=2&subtabid=3, and a rewrite rule was defined on the site level then: The rule pattern gets the URL string content/default.aspx as an input.

That means, that you shouldn't start your regexp with starting slash.

Working example is:

<rule name="RewriteImage">
    <match url="myassets/([_0-9a-z-]+)/images/category/([_0-9]+)-([_0-9a-z-]+)-([0-9]+)" />
    <action type="Rewrite" url="/myassets/{R:1}/images/category/cat_{R:2}_{R:4}_{R:3}.jpg" />
</rule>