简体   繁体   中英

IIS Rewrite does not work for the root of an application

With IIS7, URL Rewrite 2 there is an MVC4 application APP on server SRV. The following rewrite should happen.

http://SRV/APP into http://SRV/APP/

I tried by creating the AddTrailingSlash rule. However, it does not work for the application's root. It does work for directories under the root, so the following rewrite is done

http://SRV/APP/pipapo into http://SRV/APP/pipapo/

What has to be done so the rewrite also works for the root?

The following rule seems to work for me:

<!--Add trailing slash to root non-file url-->
<rule name="Add trailing slash" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <!--Match uri's missing trailing slash-->
        <add input="{PATH_INFO}" pattern="(.*[^/])$" />
        <!--Ignore any uri containing a period (probably a better way to do this but IsFile was not behaving as expected for me)-->
        <add input="{PATH_INFO}" pattern="(.*?)\.(.*?)" negate="true"/>
    </conditions>
    <action type="Redirect" redirectType="Permanent" url="{PATH_INFO}/" />
</rule>

By default, the built in add trailing slash does not apply to directories or filenames... If you want it to apply to directories (like in the above example http(s)://srv/app), you have to modify the rule and delete the Condition that has "Type: Is Not a Directory". Don't forget to apply...

Happy URL Rewriting! :)

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