Home > Net >  htaccess, formatted query string and facebook links
htaccess, formatted query string and facebook links

Time:12-08

Here is my htaccess:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ https://www.example.com/$1 [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]

all works fine:

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"

and

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

if "id"and "mk" values do not meet those rules (example id=qwe), then I drop abnormal query string and redirected it to general view.php url:

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

The only problem I am facing is Facebook links to my site, they have the following structure which I want to keep:

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

My htaccess drops the whole query string, because it thinks id=123&xxxx is already abnormally formatted query, so instead of going to particular page:

view.php?id=123

it goes to:

view.php

which is not good, because it is different content.

So, the QUESTION is how to make exception from query string normalization rules for Facebook links i.e. not to drop:

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

CodePudding user response:

You may use:

RewriteEngine on
RewriteBase /

RewriteRule ^(.*)$ https://www.example.com/$1 [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]

This will rightly consider & as termination of id or mk parameters thus will allow id=123&fbclid=IwAR05VGnAct as query string.

  • Related