简体   繁体   中英

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.

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM