【问题标题】:Remove HTML extension with web config permanently永久删除带有 Web 配置的 HTML 扩展
【发布时间】:2013-11-07 00:43:42
【问题描述】:

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

<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 个问题:

  1. 当我输入“斜线”时,它不起作用,并给我未找到的错误。例如:http://example.com/my-page/ 现在不行了,但是我把http://example.com/my-page 就可以了,所以我希望他们两个都可以工作

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

请指教。

谢谢

【问题讨论】:

    标签: html iis redirect web-config


    【解决方案1】:

    我知道这几乎是一年后,但我会尝试。我不确定我是否正确理解了您的问题,但如果我这样做了,我就使用

        <system.webServer>
         <caching>
          <profiles>
           <remove extension=".php" />
           <remove extension=".html" />
           <add extension=".html" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="23:59:59" varyByQueryString="*" />
           <add extension=".php" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:00:30" varyByQueryString="*" />
                 </profiles>
                 </caching>
                 <directoryBrowse enabled="false" />
                 <defaultDocument>
    

    然后是您正在使用的其余结束语句。

    【讨论】:

      【解决方案2】:

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

      <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.config 中即可。我也会添加完整的回复...
      【解决方案3】:

      如果您想在 Windows 10 上使用本地 IIS 服务器在每个网站上执行此操作,请安装 URL 重写模块,然后将其放入虚拟目录根目录的 web.config 中:

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
          <system.webServer>
              <rewrite>
                  <rules>
                      <remove name="Redirecting .html ext" />
                      <remove name="Hide .html ext" />
                      <rule name="Hide .html ext" enabled="true">
                          <match url="^(.*)" ignoreCase="true" />
                          <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>
                          <serverVariables />
                          <action type="Rewrite" url="{R:0}.html" />
                      </rule>
                      <rule name="Redirecting .html ext" enabled="true" stopProcessing="true">
                          <match url="^(.*).html" />
                          <conditions logicalGrouping="MatchAny">
                              <add input="{URL}" pattern="^(.*)\.html$" />
                          </conditions>
                          <serverVariables />
                          <action type="Redirect" url="{R:1}" />
                      </rule>
                  </rules>
              </rewrite>
          </system.webServer>
      </configuration>
      

      【讨论】:

      • 在重定向规则上使用更好的正则表达式更新我的回复,因此它只匹配以 html 结尾的文件,而不是(嗯)标题中带有 html 的 css 文件......
      猜你喜欢
      • 2013-02-03
      • 1970-01-01
      • 2011-07-06
      • 2013-10-07
      • 2020-01-20
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      相关资源
      最近更新 更多