我想必须遵循重写规则:
- 查询字符串必须以 choice1 或 choice2 开头
- 查询字符串仅包含一个其他值
- 域名应该是最终网址的路径
- 最终网址不应包含查询字符串。
- 最终网址应在其路径中包含查询字符串的两个值
到目前为止我已经做到了(在此处的帮助下):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(choice1|choice2)&([^&.]+) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^\.]+)\..+$ [NC]
RewriteRule ^ https://www.mysite.de/link/%1/%2/%3 [L,NE,R=302]
条件很好。 但最终的 url 不包含查询字符串的值(%1 和 %2)。 尽管我不使用 QSA-Flag,但还附加了查询字符串。
一些例子:
first-domain.de?choice1&abc => https://www.mysite.de/link/choice1/abc/first-domain
first-domain.de?choice2&xyz => https://www.mysite.de/link/choice2/xyz/first-domain
second-domain.de?choice1&ttt => https://www.mysite.de/link/choice1/ttt/second-domain
second-domain.de?choice2&xyz => https://www.mysite.de/link/choice2/xyz/second-domain
最佳答案
您可以使用此规则:
RewriteEngine On
RewriteCond %{QUERY_STRING}#%{HTTP_HOST} ^(choice1|choice2)&([^&#]+)#(?:www\.)?([^.]+)\. [NC]
RewriteRule ^ https://www.mysite.de/link/%1/%2/%3? [L,NE,R=302]
在单个条件中使用 %{QUERY_STRING}#%{HTTP_HOST}
的原因是我们只能使用最新的 RewriteCond
来捕获值 %1
、%2
、%3
等
关于regex - .htaccess RewriteRule 使用查询字符串重写条件,但不将查询字符串添加到最终网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62477081/