简体   繁体   中英

IIS URL Rewrite: Rewrite if file in specific path doesn't exist

I have a seemingly easy requirement of IIS' URL Rewrite module that I can't seem to get working for a Single Page Web App. I have a root folder content and inside there is a folder dashboard . The URL uses dashboard at the root (ie content is omitted from the URL). If a URL matches any file, I'd like it to serve that, otherwise, I'd like it to serve index.html in the dashboard folder. The only complication is that dashboard is inside the content folder in the filesystem but content doesn't exist in the URL.

Here's my folder structure:

/content/dashboard/foo.txt --> serve this for URL: /dashboard/foo.txt

/content/dashboard/assets/image.jpg --> serve this for URL: /dashboard/assets/image.jpg

/content/dashboard/index.html --> serve this for any URL that starts with /dashboard/ if there isn't a file that matches

Here are my rewrite rules:

    <rules>
        <rule name="dashboard assets" stopProcessing="true">
            <match url="^dashboard(.*)$" />
            <conditions logicalGrouping="MatchAll">
                <add input="content/{REQUEST_FILENAME}" matchType="IsFile" negate="false" />
            </conditions>
            <action type="Rewrite" url="content/dashboard{R:1}" />
        </rule>
        <rule name="dashboard app" stopProcessing="true">
            <match url="^dashboard(.*)$" />
            <action type="Rewrite" url="content/dashboard/index.html" />
        </rule>
    </rules>

Even though I have the dashboard assets conditions above, it never seems to use it and always serves index.html. How can I correct this?

{REQUEST_FILENAME} returns the full local directory path for the file requested. eg /content/dashboard/assets/image.jpg variable {REQUEST_FILENAME} is actually C:\\inetpub\\wwwroot\\content\\dashboard\\assets\\image.jpg

Remove the content/ from your condition input and it should work fine.

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