【问题标题】:IIS web.config rewrite/redirect rule with query string带有查询字符串的 IIS web.config 重写/重定向规则
【发布时间】:2021-03-09 14:47:16
【问题描述】:

我正在尝试在 web.config 文件上设置重写/重定向规则。我几乎没有使用 IIS 服务器的经验,所以我一直在在线查看解决方案,在 StackOverflow 和其他论坛上,并且已经尝试了其中的一些但没有成功。

我必须指出,我只有 FTP 访问服务器,所以我不能确定它是如何设置的(以防服务器设置对此有任何影响)。

以下是要求:

  1. 被重定向的页面可以有也可以没有“.php”,即可以是“my-page”或“my-page.php”。
  2. 重定向的目的地位于不同的域/子域
  3. 页面 URL 本身必须非常具体(“.php”除外)。比如我有两个页面,“my-page.php”和“my-page-extra.php”,但我只想重定向“my-page.php”。
  4. 原始查询字符串可以有多个不同顺序的参数(或根本没有)
  5. 我希望将查询字符串按原样传递到目的地。

所以,一个典型的重定向应该是这样的: 访问者试图到达:

https://my-domain.com/my-page.php?id=myid&param=myparam

https://my-domain.com/mypage?id=myid&param=myparam

我希望它重定向到:

https://my-sub-domain.my-new-domain.com/?id=myid&param=myparam

我有两个部分可行的解决方案:

解决方案 1:URL 有效,但未传递查询字符串

<rule name="My Redirect" stopProcessing="true">
    <match url="^my-page-slug(|\.php)(|\?.*)$" />
    <conditions logicalGrouping="MatchAll">
        <add input="{QUERY_STRING}" pattern="(.*)" />
    </conditions>
    <action type="Redirect" url="https://my-sub-domain.my-new-domain.com/{C:1}" redirectType="Permanent" />
</rule>

解决方案2:查询字符串通过了,但是URL没有那么严格所以不起作用

<rule name="My redirect" stopProcessing="true">
    <match url="^(.*)my-page-slug(.*)$"  ignoreCase="true" />
    <action type="Redirect" url="https://my-sub-domain.my-new-domain.com/{R:1}" redirectType="Permanent" />
</rule>

任何帮助将不胜感激。 谢谢。

【问题讨论】:

  • 在您的 Windows 机器上安装 IIS(例如 Windows 10 上的 IIS 10),然后安装 URL Rewrite 模块来使用它。您需要进行大量试验才能了解如何实施自己的规则,而不是复制别人的规则。也知道如何解决问题docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/…

标签: redirect iis url-rewriting web-config query-string


【解决方案1】:

你可以试试这个规则:

<rule name="redirectrule1" stopProcessing="true">
   <match url="^my-page(|\.php)$" />
      <conditions>
         <add input="{HTTP_HOST}" pattern="^my-domain.com$" />
         <add input="{HTTPS}" pattern="on" />
      </conditions>
   <action type="Redirect" url="https://my-sub-domain.my-new-domain.com/" />
</rule> 

【讨论】:

    【解决方案2】:

    感谢大家的帮助。经过大量的试验和错误,这对我有用(我很确定有更好的方法可以使用条件来完成这个,但到目前为止这已经成功了):

    <rule name="My Redirect" stopProcessing="true">
        <match url="^my-page(|\.php)(|\?.*)$" />
        <action type="Redirect" url="https://my-sub-domain.my-new-domain.com/{R:2}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      相关资源
      最近更新 更多