ASP.NET MVC和新的IIS7重写模块

Last year I noticed that there were 11 ways to get to my blog. Literally 11 different URLs and it wasn't helping me my ranking in the search engines. I wrote about this in detail and how I used ISAPI_Rewrite to fix it up.

去年,我注意到有11种方法可以访问我的博客。 从字面上看是11个不同的URL,它对我在搜索引擎中的排名没有帮助。 我对此进行了详细介绍,以及如何使用ISAPI_Rewrite对其进行了修复

Fast forward to this year and the IIS7 team has been taking advantage of IIS7's modular design to release a bunch of new modules out-of-band.

到了今年, IIS7团队一直在利用IIS7的模块化设计发布大量带外新模块

Both the newest ISAPI_Rewrite and Apache's standard mod_rewrite module uses distributed configuration files or .htaccess files.

最新的ISAPI_Rewrite和Apache的标准mod_rewrite模块都使用分布式配置文件或.htaccess文件。

Here's just part of my .htaccess file that makes sure that all the incoming URLs end up at the final canonical http://www.hanselman.com/blog/

这只是我的.htaccess文件的一部分,该文件可以确保所有传入的URL都以最终的规范http://www.hanselman.com/blog/结尾

RewriteRule /blog/default\.aspx http\://www.hanselman.com/blog/ [I,RP] 
RewriteCond Host: ^hanselman\.com
RewriteRule (.*) http\://www.hanselman.com$1 [I,RP]

RewriteCond Host: ^computerzen\.com
RewriteRule (.*) http\://www.hanselman.com$1 [I,RP]

RewriteCond Host: ^www.computerzen\.com
RewriteRule (.*) http\://www.hanselman.com/blog/ [I,RP]

After you've installed the IIS7 Rewrite module, you can bring rules in a couple ways. The nicest is by importing them directly. Notice the tree view in the screenshot below. It gets updated in as you type.

在安装IIS7 Rewrite模块之后,您可以通过两种方式引入规则。 最好的方法是直接导入它们。 注意下面的屏幕快照中的树视图。 当您键入时,它会更新。

Note that the importer only really understands rules in the mod_rewrite syntax. It doesn't fully support ISAPI_Rewrite so some things like Host: and [I] aren't supported in this release, but I'm hoping (and I've formally asked) that they'll support them for the final RTW (Release to Web). If you have ISAPI_Rewrite rules, you can either convert them then manually edit them to tidy up (what I did), or you can convert them to mod_rewrite syntax first.

请注意,导入程序仅真正理解mod_rewrite语法中的规则。 它不完全支持ISAPI_Rewrite,因此在此版本中不支持Host:和[I]之类的东西,但我希望(并且我已经正式要求)它们将为最终的RTW(发行版)提供支持。到网络)。 如果您具有ISAPI_Rewrite规则,则可以将其转换然后手动编辑以进行整理(我所做的),或者可以先将它们转换为mod_rewrite语法。

For example, in the rule importer UI I could have replaced the ISAPI_Rewrite directive "Host:" with "%{HTTP_HOST}" and "[I]" with "[NC]" (meaning case insensitive). Or, I can just edit the incorrectly imported rules.

例如,在规则导入器UI中,我可以将ISAPI_Rewrite指令“ Host:”替换为“%{HTTP_HOST}”,将“ [I]”替换为“ [NC]”(不区分大小写)。 或者,我可以编辑错误导入的规则。

This is useful for importing existing rules like mine, but it's still hard since we're talking obscure formats left and right. There's also an Add Rule wizard:

这对于导入像我的这样的现有规则很有用,但是由于我们谈论的是左右模糊的格式,因此仍然很难。 还有一个“添加规则”向导:

It's REALLY easy with the User Friendly URL dialog to interactively create mappings between the URLs your app uses and the URLs you want. See in the screenshot below how the combo-box is dynamically populated based on the example I put in the top text box?

使用“用户友好的URL”对话框,在应用程序使用的URL和所需的URL之间交互创建映射非常简单。 在下面的屏幕快照中,根据我放在顶部文本框中的示例,看到如何动态填充组合框?

The User Interface for this module is surprisingly deep in functionality. There's a Regular Expression tester built into it, which makes Regular Expressions suck by about -2.

该模块的用户界面具有令人惊讶的深层功能。 内置了一个正则表达式测试器,它使正则表达式的值降低了约-2。

ASP.NET MVC和SEO (ASP.NET MVC and SEO)

I noticed a post by Jason Young recently on ASP.NET and SEO (Search Engine Optimization). He's concerned about trailing slashes

我注意到Jason Young最近在ASP.NET和SEO (搜索引擎优化)上发表了一篇文章。 他担心斜杠

Ultimately I don't think it's that big of a deal since the URLs that your application generates are always consistent. Your app is what teaches search engines what to ask for. As long as your application is generating URLs that look the way you want them, you're cool.

最终,我认为这没什么大不了的,因为您的应用程序生成的URL始终是一致的。 您的应用可以教搜索引擎要求什么。 只要您的应用程序生成的URL看起来像您想要的样子,您就很酷。

The only real problem happens when other humans link to you and they make a mistake. Perhaps they include a trailing slash when you don't want one. Still, not a huge deal, but if you feel strongly about it, that's where a rewrite module comes in useful. I think that the Rewrite module would fit Jason's requirements.

唯一真正的问题发生在其他人链接到您并且他们犯错时。 如果您不想使用斜线,则可以在它们后面加上斜线。 仍然不是什么大问题,但是如果您对此有深刻的认识,那就是重写模块很有用的地方。 我认为Rewrite模块将符合Jason的要求。

Matt Hawley at eXcentrics World wrote a Legacy Route using ASP.NET Routing which is a clever idea as well. He could have certainly used this Rewrite Module, but ultimately as long as you're returning HTTP 301's (redirect permanent) or HTTP 302 (temporary redirect) as you fine appropriate, then use what makes you happy.

eXcentrics World的Matt Hawley使用ASP.NET Routing编写了Legacy Route ,这也是一个聪明的主意。 他当然可以使用此重写模块,但最终只要您返回适当的HTTP 301(永久重定向)或HTTP 302(临时重定向),然后使用使您满意的东西即可。

What's really important is that both these guys respect the permalink. A 404 is never a good thing.

真正重要的是,这两个人都尊重固定链接。 404永远不是一件好事。

Every site is different, but if I add a basic rule like this to my ASP.NET MVC site…

每个站点都是不同的,但是如果我将这样的基本规则添加到我的ASP.NET MVC站点中……

…and request http://localhost/rewritetest/Home/Index, the resulting HTTP Headers look like this, as the module forces the trailing backslash (of course you could also force NO backslash if it makes you happy):

…并请求http:// localhost / rewritetest / Home / Index ,生成的HTTP标头如下所示,因为该模块强制使用尾随反斜杠(当然,如果让您满意,也可以强制不使用反斜杠):

GET /rewritetest/Home/Index HTTP/1.1
Accept-Encoding: gzip, deflate
Host: localhost
Connection: Keep-Alive

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: http://localhost/rewritetest/Home/Index/
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET

I'm not a regular expression expert, but searching the web for "mod_rewrite" rules will keep you busy for next 50 years. Here my favorite reference for .htaccess and mod_rewrite rules.

我不是正则表达式专家,但是在网络上搜索“ mod_rewrite”规则将使您在接下来的50年中保持忙碌状态。 这是我最喜欢的.htaccess和mod_rewrite规则参考

Learning the basics of the IIS7 Rewrite Module:

学习IIS7重写模块的基础知识:

Functionality reference

功能参考

Video walkthrough

视频演练

Enjoy.

请享用。

翻译自: https://www.hanselman.com/blog/aspnet-mvc-and-the-new-iis7-rewrite-module

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值