iis - IIS7.5 web.config - 如何将路径与查询字符串匹配?
问题描述
我认为这可能是一个老问题,但我需要为客户的旧网站做一些重定向。他们正在使用 IIS7.5。
该网站有 3 种语言,英语是默认语言,具有以下 URL:
繁体中文: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 文档:但它没有提到对查询字符串条件的任何处理。
非常感谢
解决方案
似乎 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 重写模块。
推荐阅读
- typescript - 有条件地重定向抛出错误
- laravel-5.6 - Laravel 工厂 - 使用工厂中的状态
- scala - 如何在使用 grpc 依赖项时加载 spark ml 模型?
- parsing - 将 .yml 解析为对象(Kotlin)
- jaspersoft-studio - 无法在 Eclipse 中安装 jaspersoft studio
- php - Highcharts如何使用列标题作为X轴而不是图例
- android - 不要让活动选项不起作用
- javascript - NPM在nodejs中出现中止错误
- graphql - 在解析器中返回数据和错误
- python - 如何通过函数传递多个函数(附加参数)并将它们作为函数列表输出而不执行它们