IIS url rewrite rewriting all .asp to .html

2019-09-03 08:36发布

I need to create rules for web.config that will rewrite all requests for files with extension .html to .asp and redirect all .asp requests to .html

Example:
file_xyz.asp rewrites to file_xyz.html
directory1/file_xyz.asp rewrites to directory1/file_xyz.html
and

file_xyz.html redirects to file_xyz.asp
directory1/file_xyz.html redirects to directory1/file_xyz.asp

  1. What is the syntax for the rule
  2. Is this too broad a rule? If I should need for what ever reason to have a physical file such as file_abc.html how do I exclude it from the redirect rule?
  3. I am thinking I should just use ISAPI_Rewrite http://www.isapirewrite.com/ there seems to be a ton of resources out there for rewriting with htaccess and very little online help for using IIS 7 URL rewrite. Any thoughts and/or advice

Thanks in advance

So far this is the syntax I have for the web.config

<rule name="RewriteHTMLtoASP" stopProcessing="true">
  <match url="^([^/]+)\.html$" />
  <conditions logicalGrouping="MatchAll" />
  <action type="Rewrite" url="{R:1}.asp" />
  </rule>
 <rule name="RedirectASPtoHTML" stopProcessing="true">
    <match url="^([^/]+)\.asp$" />
     <conditions logicalGrouping="MatchAll">
     <add input="{REQUEST_METHOD}" pattern="^GET$" />
     </conditions>
     <action type="Redirect" url="{R:1}.html" appendQueryString="false" />
   </rule>

2条回答
ら.Afraid
2楼-- · 2019-09-03 08:48

Try this, u should know $ tag is the end of redirect/rewrite condition and querystrings will not accepted

<rule name="RewriteHTMLtoASP" stopProcessing="true">
              <match url="(.*).html(.*)" />
              <conditions logicalGrouping="MatchAll" />
              <action type="Rewrite" url="{R:1}.asp{R:2}" />
            </rule>
            <rule name="RedirectASPtoHTML" stopProcessing="true">
              <match url="(.*).asp(.*)" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_METHOD}" pattern="^GET$" />
              </conditions>
              <action type="Redirect" url="{R:1}.html{R:2}" appendQueryString="true" />
            </rule>
查看更多
爷、活的狠高调
3楼-- · 2019-09-03 08:49

Take a look at this article: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

You can re-write your web pages via web.config settings. If you use shared hosting I would not recommend to use ISAPI.

Let me know if it works for you.

Regards, Anvar

查看更多
登录 后发表回答