htaccess、格式化查询字符串和 facebook 链接

htaccess, formatted query string and facebook links

这是我的 htaccess:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ https://www.example.com/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/view.php$
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} ^(?!(mk=[1-9][0-9]{0,1}|id=[1-9][0-9]{0,3})$).*$
RewriteRule ^view.php$ https://www.example.com/view.php? [L,R=301]

一切正常:

1) non-www > www and http>https redirects... 
2) Checking of values of query parameters "id" and "mk", 
whether they are well formatted or not according the to following requirements below:

"id" range: 1-9999 (without leading "0" from 1 to 9999)
"id": always lowercase, no "Id" or "ID" or "iD"

"mk" range: 1-99 (without leading "0" from 1 to 99)
"mk": always lowercase, no "Mk" or "MK" or "mK"

如果“id”和“mk”值不符合这些规则(例如 id=qwe),那么我会删除异常查询字符串并将其重定向到一般 view.php url:

 https://www.example.com/view.php

我面临的唯一问题是指向我网站的 Facebook 链接,它们具有我想保留的以下结构:

https://www.example.com/view.php?id=123&fbclid=IwAR05VGnAct....

我的 htaccess 丢弃了整个查询字符串,因为它认为 id=123&xxxx 已经是格式不正常的查询,所以不要转到特定页面:

view.php?id=123

它转到:

view.php

哪个不好,因为内容不一样

因此,问题 是如何从 Facebook 链接的查询字符串规范化规则中排除,即不丢弃:

?id=123&fbclid=IwAR05VGnAct....

您可以使用:

RewriteEngine on
RewriteBase /

RewriteRule ^(.*)$ https://www.example.com/ [R=301,L]

RewriteCond %{REQUEST_URI} ^/view\.php$
RewriteCond %{QUERY_STRING} ^(?!(mk=[1-9]\d?|id=[1-9]\d{0,3})(&|$)).
RewriteRule ^ https://www.example.com%{REQUEST_URI}? [L,R=301]

这将正确地将 & 视为 idmk 参数的终止,因此将允许 id=123&fbclid=IwAR05VGnAct 作为查询字符串。