【问题标题】:Url rewrite not working when there are forward slashes inside the query value当查询值中有正斜杠时,URL 重写不起作用
【发布时间】:2018-02-27 05:52:33
【问题描述】:

我有一个如下所示的原始 URL:

https://example.com/?page=map&path=Video's

我在 IIS 管理器中创建了一个规则,将上面的内容重写为

http://h2609709.stratoserver.net/map/Video's/

这很好用。


然而,当原来的网址是这样的:

https://example.com/?page=map&path=Video's/birthday/2016

它被正确地重写为:

https://example.com/map/Video's/birthday/2016/

但是页面导致错误 404。如何让服务器将&path=Video's/birthday/2016 解释为 查询 值而不是远程文件夹路径?

问题是由查询字符串中的/ 字符引起的。

Web.config

<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
  <match url="^$" />
  <conditions>
    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
    <add input="{QUERY_STRING}" pattern="^page=([^=&amp;]+)&amp;path=([^=&amp;]+)$" />
  </conditions>
  <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
  <match url="^([^/]+)/([^/]+)/?$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  </conditions>
  <action type="Rewrite" url="?page={R:1}&amp;path={R:2}" />
</rule>

【问题讨论】:

    标签: url iis web url-rewriting web-config


    【解决方案1】:

    看起来就是你需要的:

    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
      <match url="^([^/]+)/(.*)/?$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="?page={R:1}&amp;path={R:2}" />
    </rule>
    

    我更改了正则表达式。现在{R:1} 是斜线之间的第一个单词,{R:2} 是后面的所有单词

    【讨论】:

    • 好的,谢谢。今天晚些时候我会试试。我回复你。
    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    相关资源
    最近更新 更多