.htaccess - 存在特殊参数时带参数的 htaccess 重定向 URL
问题描述
我希望得到以下任务的帮助:
我只想在调用 index.php 并给出特殊参数时使用 htaccess 重定向域。假设我有以下网址
https://www.my-domain.com/index.php?action=status-check&licence=80586f63&product=RFDE&domain=domain.tld
当这个 URL 被调用并且参数action
存在时,它应该被重定向到
https://www.other-domain.com/index.php?action=status-check&licence=80586f63&product=RFDE&domain=domain.tld
如果只是
https://www.my-domain.com/index.php?parm=4711
或者
https://www.my-domain.com/index.php
被调用或在此域中具有任何其他 URL 但没有参数action
,什么都不会发生。
有人可以帮我找到正确的条件和规则吗?!
谢谢!
解决方案
您可以在根.htaccess
文件的顶部执行以下操作:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)my-domain\.com
RewriteCond %{QUERY_STRING} (^|&)action=
RewriteRule ^index\.php$ https://www.other-domain.com/$0 [R=302,L]
当URL 参数存在于查询字符串中的任何位置(尽管您的示例仅在查询字符串的开头显示这一点?)时,此重定向www.my-domain.com/index.php
(有或没有 www 子域)。URL 路径和查询字符串都保留在重定向到. URL 参数可以有任何值。action
www.other-domain.com
action
反向引用包含模式的$0
整个匹配,即。关于成功。默认情况下会传递原始查询字符串 - 您无需在此处执行任何额外操作即可重定向到相同的查询字符串。RewriteRule
index.php
如果www.other-domain.com
在不同的服务器上并且在原始主机上没有其他域可能发生冲突,那么您也许可以删除检查HTTP_HOST
服务器变量的第一个条件。
推荐阅读
- python - 如何在二维列表中同时打印每一行和每一列?
- android - SMS Retriever API 在模拟器中不起作用
- android - 网格布局android / kotlin中单元格的高度和宽度相同
- openedge - 将带有科学记数法字段的 CSV 数据导入 Open Edge
- scala - 在 Spark 中将来自不同数据集的两列组合成一张表
- c - 尝试移交指向函数的指针时如何修复“预期的*但参数的类型为**”错误
- javascript - 在 iFrame 中隐藏元素
- css - 在其侧边栏父级之外显示下拉菜单,并在滚动时使菜单也根据其父级滚动,
- angular - 将 Angular 6 应用程序部署到 github 页面时出现 404
- php - 正则表达式:删除所有多余的空格、换行符和空格