URL重写无法正常更改(/ blog / category /)到(/)

时间:2018-03-01 15:39:37

标签: c# asp.net url-rewriting nopcommerce

在web配置中,我编写了以下代码

 <system.webServer>
    <rewrite>
      <rules>
        <rule name="catalogsmain" stopProcessing="true">
          <match url="\/blog\/category\/" />
          <action type="Redirect" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

还试过

<system.webServer>
    <rewrite>
      <rules>
        <rule name="catalogsmain" stopProcessing="true" patternSyntax="ExactMatch">
          <match url="/blog/category/" />
          <action type="Redirect" url="/" />
        </rule>        
      </rules>
    </rewrite>
  </system.webServer>

当我浏览网址时 http://localhost:55390/blog/category/healthy-living-and-advice

它没有变为

http://localhost:55390/healthy-living-and-advice

如何重写网址?

我想改变

http://mitesh.com/blog/category/healthy-living-and-advice

http://mitesh.com/healthy-living-and-advice

1 个答案:

答案 0 :(得分:0)

您需要捕获blog/category后面的网址部分 另外我不确定url在开始时是否包含斜杠。所以试试这个

<system.webServer>
 <rewrite>
  <rules>
    <rule name="catalogsmain" stopProcessing="true">
      <match url="\/?blog\/category\/(.*)" />
      <action type="Redirect" url="/{R:1}" />
    </rule>
  </rules>
 </rewrite>
</system.webServer>

正则表达式中的问号就在那里,因为我不确定斜线。但原则是您捕获了网址的一部分,{R:1}是对第一个捕获组内容的引用。