Removing the trailing slash from a nested application's virtual directory root in IIS

荒凉一梦 提交于 2019-12-19 04:14:16

问题


I have a website with a nested application. Say the virtual directory is '/app' for the nested application. When a request is made to www.mysite.com/app, a redirect is made to www.mysite.com/app/, probably because the virtual directory is not empty. I have a rule set up in the nested app's web.config to remove trailing slashes from all urls, but this does not affect the root path of the nested application. Is there a way (maybe a setting in IIS) to rewrite or redirect to the root without the trailing slash? I have tried many rewrite and redirect rules to no avail.

Here is my rule for removing trailing slashes:

<rule name="Remove trailing slash" stopProcessing="true">
  <match url="(.*)/$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>

Like I said, this works great, but this does NOT affect the root. There is a default.aspx page in the root that I want to load from www.mysite.com/app, NOT www.mysite.com/app/.

Is there a work around for this?


回答1:


I may finally have an answer. This is on Windows Server 2008 R2, IIS 7.5.

Solution, in IIS:

  • Disable the Default Document feature for the child application.
  • Using Url Rewrite, create a rule to rewrite (not redirect) an empty request to default.aspx

Here is the rule in web.config, and the entry disabling the default document. I used the UI to do these, and this is what it created:

<defaultDocument enabled="false" />
<rewrite>
    <rules>
        <rule name="No slash root" enabled="true" stopProcessing="true">
            <match url="^$" />
            <action type="Rewrite" url="default.aspx" />
        </rule>
    </rules>
</rewrite>

The problem seems to stem from having a default document. When I disabled the default document, it changed from redirecting, to a 403 forbidden, which is the usual directory browsing attempt. Then adding a rule to rewrite the blank request ran the default page, without changing the browser url.

I just disabled the Default Document entirely. Possibly could just remove the default.aspx from the default document list, but I didn't try that.

The empty request rule is picking up the www.mysite.com/app with nothing else, and it executes the default.aspx page.

I did some tests to make sure the page was still executing in the child application. I just wanted to make sure it wasn't somehow magically thinking this was a file in the root application. I did this by creating various .aspx files to write and read good old Application["abc"] variables. The child app default file could read the variable written by another file in the child app. A root-level file did not see the variable.

For further rules, I use Helicon Ape. I started with their software years ago, and I prefer the htaccess syntax over the IIS syntax. I tested this using the rule above in IIS to avoid the trailing slash, and Ape/htaccess for all my remaining rules, and this seems to be working.

I made many attempts to get Ape alone to avoid the redirect, to no avail. It seems IIS goes first, so this rule needs to be in IIS, but remaining rules do not.

Note: When trying these rules, your browser tends to cache the redirects. Work in "incognito mode" (Chrome), and/or clear the browser cache every time. See https://stackoverflow.com/a/9204355/292060



来源:https://stackoverflow.com/questions/22383619/removing-the-trailing-slash-from-a-nested-applications-virtual-directory-root-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!