永久删除带有Web配置的HTML扩展名 [英] Remove HTML extension with web config permanently

查看:90
本文介绍了永久删除带有Web配置的HTML扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用web.config从页面中删除html扩展名.下面是我在web.config文件中使用的代码

I am trying to remove html extension from pages using web.config. Below is the code i am using in web.config file

<rewrite>
  <rules>
    <rule name="rewrite html">
      <match url="(.*)$" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).html" />
      </conditions>
      <action type="Rewrite" url="{R:1}.html" />
    </rule> 
  </rules>
</rewrite>

它可以正常工作并删除html扩展名,但是这里似乎有2个问题:

it is working fine and removing html extension, however there seems to be 2 problems here :

  1. 当我放斜杠"时,它不起作用,并给我未找到的错误.例如:http://example.com/my-page/现在它将无法正常工作,但是我放http://example.com/my-page它将正常工作,所以我希望他们两个都可以工作

  1. When i put 'slash' it does not work and gives me not found errors. For example: http://example.com/my-page/ now it will not work, but I put http://example.com/my-page then it will work fine, so i would like to both of them to work

另一个问题是.html页面仍在打开.例如,如果我以http://example.com/my-page.html的身份打开该页面,但它也可以正常工作,但是我希望它可以自动转换为http://example.com/my-page,我知道我可以为此使用301重定向,但这将不起作用,因为这里有很多文件,因此我必须对不同的URL使用不同的301规则.

Other problem is that .html pages are still opening. For example, if I open the page as http://example.com/my-page.html it is also working but I want it to convert to http://example.com/my-page automatically, I know I can use 301 redirects for this but that will not work as there are many of files here, so I have to use different 301 rules for different URLs.

请咨询.

谢谢

推荐答案

URLRewrite 2.0规则(将此部分插入system.webServer节点内),该规则将从url中替换.html:

URLRewrite 2.0 rule (insert this part inside system.webServer node) that replaces .html from url:

<rewrite>
    <rules>
        <rule name="Hide .html ext">
            <match ignoreCase="true" url="^(.*)"/>
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                <add input="{REQUEST_FILENAME}.html" matchType="IsFile"/>
            </conditions>
            <action type="Rewrite" url="{R:0}.html"/>
        </rule>
        <rule name="Redirecting .html ext" stopProcessing="true">
            <match url="^(.*).html"/>
            <conditions logicalGrouping="MatchAny">
                <add input="{URL}" pattern="(.*).html"/>
            </conditions>
            <action type="Redirect" url="{R:1}"/>
        </rule>
    </rules>
</rewrite>

这篇关于永久删除带有Web配置的HTML扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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