如何使用 web.config 中的重写规则从 www.example.com/section/index.aspx 重定向到 www.example.com/section?它还必须适用于各种级别,例如 www.example.com/parent/child
*请注意,我无权访问服务器。我基本上可以只编辑 web.config 文件并告诉服务器重建应用程序。
您最好的选择是使用IIS7 URL 重写模块- 但您需要在服务器上安装它。它非常易于使用且功能强大。如果您是托管的,它可能已经安装,因为虽然默认情况下没有安装它,但它来自 Microsoft 并且非常常用。
如果您使用的是 2.0 或更高版本的 asp.net,则可以urlMappings
在 web.config 中添加一个部分:
<system.web>
<urlMappings enabled="true">
<add url="~/Section" mappedUrl="~/Section/index.aspx"/>
</arlMappings>
</system.web>
但这有一些问题:首先,如果请求的 URL 没有由 ASP.Net 模块处理,或者没有传递到您的应用程序,则永远不会发生重写。例如,这可能是因为您没有点击“.aspx”文件。此外,在某些配置中,您请求的文件需要存在。另一个问题是不支持通配符规则,因此您必须添加规则以单独重写所有可能的路径。
最后,您可以将 asp.net rewrite httpmodules 放到 bin 目录中并添加到您的 web.config 中。这是 ScottGu 的一些(可能已经过时的)选项,用于url rewriting。
这可能非常令人作呕,但是通过为每个可能的级别创建规则,我能够重写从 url 删除 index.aspx 的所有路径。
从...开始
<rule name="Migrate to PHP">
<match url="^([_0-9a-z-]+).aspx"/>
<action type="Redirect" redirectType="Permanent" url="/"/>
</rule>
并以
<rule name="Migrate to PHP all the way">
<match url="^([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+).aspx"/>
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}/{R:3}/{R:4}"/>
</rule>