iis url 重写模块2_使用IIS重写模块将ASP.NET旧版URL重定向到无扩展名

本文介绍了如何使用IIS URL重写模块将现有的ASP.NET旧版URL(如foo.html)重定向到无扩展名的URL(如/foo),同时保持内部重写以处理请求。这种方法不仅适用于ASP.NET,还适用于IIS能处理的任何文件类型。文章提供了web.config的示例配置,并强调了测试和避免意外捕获CSS或JS资源的重要性。
摘要由CSDN通过智能技术生成
iis url 重写模块2

iis url 重写模块2

ASP.NET has included support for "friendly URLs" for a while now. ASP.NET MVC has always supported friendly URLs and more recently, so has Web Forms. That means if you don't want to have the .aspx extension, you certainly don't have to.

ASP.NET现在已经包含了对“友好URL”的支持。 ASP.NET MVC一直以来都支持友好的URL,而Web窗体也一直支持。 这意味着,如果您不想使用.aspx扩展名,则当然不需要。

However, there's a LOT of existing legacy apps out there as well as apps that you may not have full control over. For example, there's a site that I want to influence but it's got dozens (hundreds) of links to foo.html and bar.html existing pages.

但是,那里有很多现有的旧版应用程序以及您可能无法完全控制的应用程序。 例如,有一个我想影响的网站,但它有数十个(数百个)指向foo.html和bar.html现有页面的链接。

Legacy (n): A super-fancy way of saying "already exists."

旧版(n):说“已经存在”的超级幻想方式。

What I want to do is kind of sloppy and I'm doing it for aesthetic reasons. I'll hopefully get around to updating the site's links later and know that future links will be extensionless. But look at me, I'm justifying why I'm doing this, Dear Reader. You of all people know that sometimes you just gotta do something just because ya gotta Get It Done™©.

我想做的是草率的,出于审美原因,我正在做。 我希望以后会更新站点的链接,并且知道以后的链接将是无扩展的。 但是,看着我,我在说明为什么要这么做,亲爱的读者。 所有人中的所有人都知道,有时候您只是因为做某事而不得不做某事。©©。

I want to:

我想要:

  • redirect existing GETs to a /foo.html to /foo

    现有GET重定向到/foo.html重定向到/ foo

    • Redirects are external

      重定向是外部的

    redirect existing GETs to a /foo.html to /foo

    现有GET重定向到/foo.html重定向到/ foo

  • but, keep rewriting /foo to the underlying /foo.html so it handles the request

    但是,请继续/ foo重写为基础的/foo.html,以便其处理请求

    • Rewrites are internal

      重写是内部的

    but, keep rewriting /foo to the underlying /foo.html so it handles the request

    但是,请继续/ foo重写为基础的/foo.html,以便它处理请求

I can do all this within my web.config using the IIS Url Rewrite Module.  I can do this with ANY file type that IIS can handle, meaning this isn't an ASP.NET-specific thing. This all happens well before your application gets involved. You'll note I did a similar thing with a PHP app running under IIS just last month.

我可以使用IIS网址重写模块在web.config中完成所有这些操作。 我可以使用IIS可以处理的任何文件类型来执行此操作,这意味着这不是ASP.NET特有的。 这一切都发生在您的应用程序参与之前。 您会注意到,上个月我对在IIS下运行PHP应用程序做了类似的事情。

Here's what my web.config looks like. Note that since I have Azure (or in just IIS 7+ and the Rewrite module) I just added this file. There was no configuration needed. The same would apply to any existing site. Be aware that sometimes super-"greedy" rewrite or redirect rules can end up grabbing ahold of your CSS or JS so you'll want to be aware if something odd happens.

这是我的web.config的样子。 请注意,由于我具有Azure(或仅在IIS 7+和“重写”模块中),因此我仅添加了此文件。 无需配置。 这同样适用于任何现有站点。 请注意,有时超级“贪婪”的重写或重定向规则最终可能会抢占您CSS或JS,因此您需要知道是否发生了奇怪的事情。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="extensionless" stopProcessing="true">
<match url="(.*)\.html$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="removeextension" enabled="true">
<match url=".*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

This stuff is hard to write, though.

但是,这些东西很难写。

SIDE NOTE: My RegEx expert Ruslan points out that the final Action could be simplified slightly like this:

旁注:我的RegEx专家Ruslan指出,最终的操作可以这样简化:

<add input="{URL}" pattern="\." negate="true" />

This stuff is also hard to test. The IIS Rewrite module has a great UI for IIS that will write a lot of these rules for you and let you test them interactively:

这些东西也很难测试。 IIS Rewrite模块为IIS提供了一个很棒的UI,它将为您编写许多规则,并让您以交互方式对其进行测试:

Hope this helps. I wrote this post (and bookmarked it) for myself because I am always googling around for this particular rule to remind myself. Now I'll search my own blog. ;) Reason #64 to blog, friends.

希望这可以帮助。 我为自己写了这篇文章(并为它加了书签),因为我总是在谷歌周围搜索这个特殊的规则来提醒自己。 现在,我将搜索自己的博客。 ;)原因#64,博客,朋友。

SPONSOR: Big thanks to the feed sponsor this past week, Ext.NET (seriously, check out their demos, really amazing stuff!) - Quickly build modern WebForm and MVC (including RAZOR) Apps for ASP.NET. Free pancake breakfast with all purchases!

发起人:非常感谢过去一周的feed赞助商Ext.NET (认真地查看他们的演示,真是太神奇了!) -快速为ASP.NET构建现代的WebForm和MVC(包括RAZOR)应用程序。 所有购买均可免费获赠煎饼早餐!

翻译自: https://www.hanselman.com/blog/redirecting-aspnet-legacy-urls-to-extensionless-with-the-iis-rewrite-module

iis url 重写模块2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值