首页 > 解决方案 > 存在特殊参数时带参数的 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,什么都不会发生。

有人可以帮我找到正确的条件和规则吗?!

谢谢!

标签: .htaccessmod-rewriteurl-rewriting

解决方案


您可以在根.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 参数可以有任何值。actionwww.other-domain.comaction

反向引用包含模式$0整个匹配,即。关于成功。默认情况下会传递原始查询字符串 - 您无需在此处执行任何额外操作即可重定向到相同的查询字符串。RewriteRule index.php

如果www.other-domain.com在不同的服务器上并且在原始主机上没有其他域可能发生冲突,那么您也许可以删除检查HTTP_HOST服务器变量的第一个条件。


推荐阅读