Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS Rewrite rule - check if a static file exists

Can someone tell me how to check if a static file exists as a rewrite rule condition.

I'm on IIS8, and my web app is it's own web site. I want to add a rewrite rule that checks if afile exists, and if so then applies the rewrite. Here's my code:

<rewrite>
    <rules>
        <rule name="Find static gravatar" stopProcessing="true">
            <match url="^images/animage.png$" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                <add input="{QUERY_STRING}" pattern="i=(.+)" />
                <add input="/favicon.ico" matchType="IsFile" />
            </conditions>
            <action type="Rewrite" url="/Somewhere/Else/images/{C:1}.png" appendQueryString="false" logRewrittenUrl="true" />
        </rule>
    </rules>
</rewrite>

Hitting URL /images/animage.png?i=SS

In desperation I'm checking for a file I know exists - favicon.ico. If I comment out the 2nd condition that checks for the file, it works. With the condition in, it fails.

like image 943
Matt Roberts Avatar asked Nov 10 '22 07:11

Matt Roberts


1 Answers

As it works without the <add input="/favicon.ico" matchType="IsFile" /> line, that line is suspect to me.

I'd have to guess it has to do with your /favicon.ico path, as the matchType="IsFile" is correct.

See this link for more examples, their resolution was to not use a forward slash (/) but instead use a backslash (\).

But I must to use not slash (/) in line <add input="{DOCUMENT_ROOT}/{R:1}" matchType="IsDirectory" negate="true" />. I must to use BACKSLASH (). That line is right: <add input="{DOCUMENT_ROOT}{R:1}" matchType="IsDirectory" negate="true" />

like image 138
Brock Hensley Avatar answered Nov 15 '22 07:11

Brock Hensley