Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Apache rewrite condition for matching Query

I need a rewrite condition for returning e.g. 403, when a query contains characters, which are not in my "whitelisted" character class. The query beginns with q= So my attempt was:

RewriteCond %{REQUEST_URI} ^/generic/
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} q=[^a-z]+
RewriteRule .* - [F]

It is working almost. Only problem is, when the query beginns with e.g. "a", any following blacklisted characters don't trigger the rule

Example:

q=abc -> rule does not match -> fine
q=Abc -> rule matches -> fine
q=abC -> rule does not match -> not fine

So when the forbidden characters are not the first character after "q=", the rule is not working.

What am I missing? Can someone help me?

BR

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

With your shown attempts, please try following htaccess rules file. 2 important points here, 1st you can use ^ in spite of .* in last forbidding Rule. 2nd is you need to use NC to cover lower case to match small and capital letters in regex.

RewriteCond %{REQUEST_URI} ^/generic/  [NC]
RewriteCond %{REQUEST_METHOD} ^GET$ [NC]
RewriteCond %{QUERY_STRING} q=[^a-z]+ [NC]
RewriteRule ^ - [F]

OR use following rules to match exact query string use ^ and $ anchors to match exact query string value:

RewriteCond %{REQUEST_URI} ^/generic/  [NC]
RewriteCond %{REQUEST_METHOD} ^GET$ [NC]
RewriteCond %{QUERY_STRING}  ^q=[^a-z]+$ [NC]
RewriteRule ^ - [F]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!