从 IIS 中嵌套应用程序的虚拟目录根中删除尾部斜杠 [英] Removing the trailing slash from a nested application's virtual directory root in IIS

查看:16
本文介绍了从 IIS 中嵌套应用程序的虚拟目录根中删除尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有嵌套应用程序的网站.假设嵌套应用程序的虚拟目录是/app".当向 www.mysite.com/app 发出请求时,会重定向到 www.mysite.com/app/,可能是因为虚拟目录不为空.我在嵌套应用程序的 web.config 中设置了一个规则,用于从所有 url 中删除尾部斜杠,但这不会影响嵌套应用程序的根路径.有没有办法(可能是 IIS 中的设置)在没有尾部斜杠的情况下重写或重定向到根目录?我尝试了很多重写和重定向规则都无济于事.

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>

就像我说的,这很好用,但这不会影响根.根目录中有一个 default.aspx 页面,我想从 www.mysite.com/app 加载,而不是 www.mysite.com/app/.

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?

推荐答案

我可能终于有了答案.这是在 Windows Server 2008 R2、IIS 7.5 上.

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

解决方案,在 IIS 中:

Solution, in IIS:

  • 禁用子应用程序的默认文档功能.
  • 使用 Url Rewrite,创建一个规则来重写(而不是重定向)一个空请求到 default.aspx

这是 web.config 中的规则,以及禁用默认文档的条目.我使用 UI 来执行这些操作,这就是它创建的内容:

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>

问题似乎源于有一个默认文档.当我禁用默认文档时,它从重定向更改为 403 禁止,这是通常的目录浏览尝试.然后添加规则重写空白请求运行默认页面,不更改浏览器url.

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.

我只是完全禁用了默认文档.可能只是从默认文档列表中删除 default.aspx,但我没有尝试.

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.

空请求规则是在没有别的东西的情况下选择 www.mysite.com/app,并执行 default.aspx 页面.

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

我做了一些测试以确保页面仍在子应用程序中执行.我只是想确保它不会以某种方式神奇地认为这是根应用程序中的一个文件.我通过创建各种 .aspx 文件来写入和读取旧的 Application["abc"] 变量来做到这一点.子应用默认文件可以读取子应用中另一个文件写入的变量.根级文件没有看到该变量.

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.

对于进一步的规则,我使用 Helicon Ape.几年前我开始使用他们的软件,我更喜欢 htaccess 语法而不是 IIS 语法.我在 IIS 中使用上面的规则对此进行了测试,以避免尾随斜杠,并使用 Ape/htaccess 测试我所有剩余的规则,这似乎有效.

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.

我多次尝试让 Ape 独自一人以避免重定向,但无济于事.好像是 IIS 先行,所以这条规则需要在 IIS 中,但其余规则不需要.

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.

注意:尝试这些规则时,您的浏览器往往会缓存重定向.在隐身模式"(Chrome)下工作,和/或每次都清除浏览器缓存.请参阅https://stackoverflow.com/a/9204355/292060

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

这篇关于从 IIS 中嵌套应用程序的虚拟目录根中删除尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆