IIS url重写|如何删除目录和扩展名? [英] IIS url rewrite | How to remove directory and extension?

查看:121
本文介绍了IIS url重写|如何删除目录和扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决以下问题:

I have been struggling with the following for quite some time now:

默认网址:

examplesite.com/folder/about.cshtml

所需的网址:

examplesite.com/about

基本上我想完成两件事:

Basically I want to accomplish two things:


  • 1使用实际紧凑的代码删除文件扩展名。

  • 2删除包含about页面的文件夹。

我找到了一些不常见的规则来实现所有目标以上,但它们主要包含大量冗余代码,当我用IIS 8.0测试它时会崩溃我的网站。

I have found some uncommon rules to achieve all the above, but they mostly contain a lot of redundant code that crashes my site when I test it with IIS 8.0.

所以我希望有人可以分享一个规则紧凑,符合我的需求。或者用相同的结果分开规则。

So I was hoping someone could share a rule that is compact and fits my needs. Or seperate rules with the same outcome.

非常感谢每一个贡献:)

Every contribution is much appreciated :)

推荐答案

我不确定我是否完全理解你的需求,但这里的东西至少是接近的。它删除了第一个文件夹和文件扩展名(所以 examplesite.com/folder/about.cshtml 变为 examplesite.com/about examplesite.com/folder/help/about.cshtml 变为 examplesite.com/help/about )。如果您要删除所有文件夹,只需删除

I'm not certain I entirely understand your needs, but here's something that's at least close. It strips out the first folder and file extension (so examplesite.com/folder/about.cshtml becomes examplesite.com/about and examplesite.com/folder/help/about.cshtml becomes examplesite.com/help/about). If you wanted to strip all folders then just remove the ?.

<rule name="Remove Directory and Extension">
    <match url="^(.*?)/(.*)\.cshtml$" />
    <action type="Rewrite" url="{R:2}" />
</rule>

更新:

好的,我认为您想要的是两个规则的组合:

Ok, I think what you want is a combination of two rules then:

<rules>
  <rule name="Redirect requests to friendly URLs">
    <match url="^(.*?)/(.*)\.cshtml$" />
    <action type="Redirect" url="{R:2}" />
  </rule>
  <rule name="Rewrite friendly URLs to phsyical paths">
    <match url="^(.*)$" />
    <action type="Rewrite" url="folder/{R:0}.cshtml" />
  </rule>
</rules>

第一条规则确保所有请求都是友好的URL。第二个获取友好URL并将其重写到您的物理路径,其中物理路径为文件夹/ [FRIENDLY_PATH] .cshtml

The first rule makes sure that all requests are to friendly URLs. The second takes the friendly URL and rewrites it to your physical path, where the physical path is folder/[FRIENDLY_PATH].cshtml.

这篇关于IIS url重写|如何删除目录和扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆