如何使用web.config从URL中删除index.aspx?

时间:2010-11-22 15:48:01

标签: asp.net redirect rackspace

如何使用web.config中的重写规则从www.example.com/section/index.aspx重定向到www.example.com/section?它还必须适用于各种级别,例如www.example.com/parent/child

*注意到我无法访问服务器。我基本上只需编辑web.config文件并告诉服务器重建应用程序。

2 个答案:

答案 0 :(得分:4)

您最好的选择是使用IIS7 URL Rewrite Module - 但您需要在服务器上安装它。它非常易于使用且功能强大。如果你是托管的,它可能已经被安装了,因为虽然默认情况下它没有安装,但它来自微软并经常使用。

如果您使用的是asp.net的2.0或更高版本,则可以在web.config中添加urlMappings部分:

<system.web>
    <urlMappings enabled="true">
        <add url="~/Section" mappedUrl="~/Section/index.aspx"/>
    </arlMappings>
</system.web>

但是这有一些问题:首先,如果请求的URL不是由ASP.Net模块处理的,或者没有传递给您的应用程序,则重写永远不会发生。这可能是因为您没有点击“.aspx”文件,例如。此外,在某些配置中,您请求的文件需要存在。另一个问题是没有支持通配符规则,因此您必须添加规则以单独重写所有可能的路径。

最后,有一些asp.net重写httpmodules你可以放入bin目录并添加到你的web.config。以下是ScottGu对url rewriting的一些(可能是过时的)选项。

答案 1 :(得分:0)

这可能非常恶心,但通过为每个可能的级别创建规则,我能够重写从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>