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

发布于 2024-10-03 09:59:20 字数 197 浏览 5 评论 0 原文

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

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

How would one redirect from www.example.com/section/index.aspx to www.example.com/section using rewrite rules in web.config? It would also have to work for various levels such as www.example.com/parent/child

*Noting that I do not have access to the server. I can basically just edit the web.config file and tell the server to rebuild the application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-10-10 09:59:20

您最好的选择是使用 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 重写 httpmodules 放入 bin 目录中并添加到您的 web.config 中。以下是 ScottGu 为 网址重写

Your best bet is to use the IIS7 URL Rewrite Module - but you would need to install this on the server. It's pretty easy to use and powerful at the same time. It may already be installed if you're hosted, because although it's not installed by default, it is from Microsoft and pretty frequently used.

If you're on 2.0 or greater of asp.net, you can add a urlMappings section to the web.config:

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

But this has some issues : Firstly, if the URL requested isn't handled by the ASP.Net module, or isn't delivered to your application, the rewrite never happens. This could occur because you aren't hitting a ".aspx" file, for example. Also, in some configurations, the file you request needs to exist. Another issue is that there are no wildcard rules supported, so you would have to add rules to rewrite all possible paths individually.

And finally, there are asp.net rewrite httpmodules you could drop in the bin directory and add to your web.config. Here's some (possibly outdated) options by ScottGu for url rewriting.

深空失忆 2024-10-10 09:59:20

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

This is probably massively disgusting but by creating rules for each possible level I was able to rewrite all paths dropping index.aspx from the url.

starting with

<rule name="Migrate to PHP">
 <match url="^([_0-9a-z-]+).aspx"/>
 <action type="Redirect" redirectType="Permanent" url="/"/>
</rule>

and ending with

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文