简体   繁体   English

重定向 .htaccess 中的 URL,但保留查询字符串

[英]Redirect a URL in .htaccess, but keep the query string

I want to redirect a URL in .htaccess, but want to keep the (dynamic) parameters from the query string at the end of the URL (eg ?id=1660 , ?id=1661 , etc.)我想重定向 .htaccess 中的 URL,但想保留 URL 末尾的查询字符串中的(动态)参数(例如?id=1660?id=1661等)

Eg https://mywebsite.example/service/viewinvoice.php?id=1660例如https://mywebsite.example/service/viewinvoice.php?id=1660

I want to redirect it to:我想将其重定向到:

https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=1660

So basically: https://mywebsite.example/service/viewinvoice.php?id=... needs to be redirected to https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...所以基本上: https://mywebsite.example/service/viewinvoice.php?id=...需要重定向到https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...

I tried this below, without any success我在下面尝试了这个,没有任何成功

RewriteEngine On

RewriteCond %{QUERY_STRING} (^|&)/service/viewinvoice.php?id= [NC]
RewriteRule ^/?$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301]

I think this is not the right solution.我认为这不是正确的解决方案。 Does someone has suggestions?有人有建议吗?

You need to use the QSA (Query String Append) flag on your rewrite rule.您需要在重写规则上使用QSA (查询字符串附加)标志。 From the documentation:从文档中:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one.当替换 URI 包含查询字符串时,RewriteRule 的默认行为是丢弃现有的查询字符串,并将其替换为新生成的查询字符串。 Using the [QSA] flag causes the query strings to be combined.使用 [QSA] 标志会合并查询字符串。

From your example URLs, you don't need to match the query string in a rewrite condition.从您的示例 URL 中,您不需要在重写条件中匹配查询字符串。 You are matching the URL path which is done as the first part of the rewrite rule itself.您正在匹配作为重写规则本身的第一部分完成的 URL 路径。

Your rule should be:你的规则应该是:

RewriteEngine On
RewriteRule ^/?service/viewinvoice\.php$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM