首页 > 解决方案 > IIS7.5 web.config - 如何将路径与查询字符串匹配?

问题描述

我认为这可能是一个老问题,但我需要为客户的旧网站做一些重定向。他们正在使用 IIS7.5。

该网站有 3 种语言,英语是默认语言,具有以下 URL:

英文:https ://olddomain.com

繁体中文:https ://olddomain.com/index.php?q=zh-hant

简体中文:https ://olddomain.com/index.php?q=zh-hans

请注意,该网站还有其他部分,例如“关于我们”。旧的丑陋结构是:

英文:https ://olddomain.com/index.php?q=about-us

繁体中文:https ://olddomain.com/index.php?q=zh-hant/about-us

简体中文:https ://olddomain.com/index.php?q=zh-hans/about-us

实际上,只需将所有旧站点的主页和/或部分重定向到新站点的主页就足够了。

我试图添加和使用:

        <httpRedirect enabled="true" exactDestination="true">
        <add wildcard="/index.php?q=zh-hans*" destination="https://newsitedomain.com/sc/somename" />
        <add wildcard="/index.php?q=zh-hant*" destination="https://newsitedomain.com/tc/somename" />
        <add wildcard="*" destination="https://newsitedomain.com/en/somename" />
    </httpRedirect>

英文版很好,但所有其他 TC 和 SC 版本也都转到 EN 新站点。

我的配置有什么问题?

我已经阅读了Microsoft 文档:但它没有提到对查询字符串条件的任何处理。

非常感谢

标签: iisiis-7.5

解决方案


似乎 IIS httpRedirect 无法处理 URL 的查询字符串。

这是一种解决方法:

<!-- redirect base URL -->
<system.webServer>
  <httpRedirect enabled="true" exactDestination="true">
    <add wildcard="/" destination="https://newsitedomain.com/en/somename" />
  </httpRedirect>
<system.webServer>
<!-- custom redirect by _redirect.html (or your custom php) -->
<location path="index.php">
  <system.webServer>
    <httpRedirect enabled="true" destination="_redirect.html$Q" exactDestination="true" />
  </system.webServer>
</location>

除了 web.config,您还需要以下 _redirect.html 文件

<html>
  <head>
    <script>
      if (window.location.href.indexOf('q=zh-hans') !== -1) {
        window.location.href = 'https://newsitedomain.com/sc/somename';
      } else if (window.location.href.indexOf('q=zh-hant') !== -1) {
        window.location.href = 'https://newsitedomain.com/tc/somename';
      } else {
        window.location.href = 'https://newsitedomain.com/en/somename';
      }
    </script>
  </head>
</html>

另一种选择是使用 URL 重写模块。


推荐阅读