nitmws
nitmws

Reputation: 41

Require minimal length of key in query params with .htaccess

The keys of query params of URLs should have a minimal length, e.g. 6 characters, to be accepted. This should be filtered by .htaccess.

Example of URL being ok: https://example.com/blogs/?thisisalongkey=134234

Example of URL having a too short key - should be stopped: https://example.com/blogs/?shkey=134234

Is it possible to check the length of a key only?

I've tried this in .htaccess but it didn't work, also long keys were "bad" ones and stopped.

RewriteCond %{QUERY_STRING} ^[a-z][a-z0-9]{1,5} [NC]
RewriteRule ^ - [F]

Upvotes: 0

Views: 23

Answers (1)

nitmws
nitmws

Reputation: 41

Ok, searching for regex and "limiting string length" helped to find a regex pattern for checking if the length is in a set range.

And checking the length of the key only requires to split key and value, separated by a =.

These .htaccess lines work = a 403 is returned if ...

  • the length-to-be-checked key is the 1st parameter
  • only \w characters are in the key
  • this key is 1 to 5 characters long
RewriteCond %{QUERY_STRING} ^(\w{1,5})=(.*)$ [NC]
RewriteRule .* - [F,L]

Upvotes: 0

Related Questions