Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

regex - URL Encoded text not matching htaccess QUERY_STRING

My website uses a comma to separate some coordinates in a parameter, like ?coords=38.88,-77.03. I'm now finding issues as , is a url reserved character. The issue seems limited to when the links are shared with Facebook and Facebook changing it to ?coords=38.88%2C-77.03.

Long term I'd like to fix it but I need to make a temporary fix for existing links that have been shared. If people were to click the link from Facebook I'd like an .htaccess rule to change it to using the comma.

I already have a similar rule for when there's a space after the comma:

RewriteCond %{QUERY_STRING} (.*coords=-?d{1,2}(?:.d+)?),(?:s+|\%20|+)(-?d{1,3}(?:.d+)?,?.*) [NC]
RewriteRule ^ %{REQUEST_URI}?%1,%2 [L,NE,R=301]

I tried changing it to:

RewriteCond %{QUERY_STRING} (.*coords=-?d{1,2}(?:.d+)?)(,|\%2C)(?:s+|\%20|+)(-?d{1,3}(?:.d+)?,?.*) [NC]
RewriteRule ^ %{REQUEST_URI}?%1,%3 [L,NE,R=301]

I cleared my cache and even tried changing the coordinate numbers to new ones, but I can't get it to redirect. What am I doing wrong and how can I fix it?

question from:https://stackoverflow.com/questions/65546374/url-encoded-text-not-matching-htaccess-query-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your earlier non-capture group is not optional thus it is trying to match 1+ spaces after matching %2C and failing. You may use this rule:

RewriteCond %{QUERY_STRING} (.*coords=-?d{1,2}(?:.d+)?)(?:[+s]+|\%2C|\%20)+(-?d{1,3}(?:.d+)?,?.*) [NC]
RewriteRule ^ %{REQUEST_URI}?%1,%2 [L,NE,R=301]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...