Remove HTML extension with web configuration permanently

I am trying to remove the html extension from pages using web.config. Below is the code that I use in the 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>

it works fine and removes the html extension, however, there seem to be 2 problems here:

  • When I put a 'slash', it does not work and does not cause errors. For example: http://example.com/my-page/now this will not work, but I put it http://example.com/my-page, then it will work fine, so I would like them both to work

  • , .html . , http://example.com/my-page.html, , , http://example.com/my-page, , 301 , , , 301 URL-.

, .

+4
2

URLRewrite 2.0 ( system.webServer node), .html 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>
+12

, , . , ,

    <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>

, .

0

All Articles