htaccess mod_rewrite for specific starting substrings only

OK, when reading this question please ignore the fact that the developers should be fixing their out of date plugins... this is another battle being fought elsewhere.

We are trying to block querystrings in the format of mydomainname.com/?q=anytexthere

I have successfully used this code in the htaccess file:

RewriteCond %{QUERY_STRING} \bq=\b [NC]
RewriteRule ^ - [F]

Open in new window


But this unfortunately stops timthumb.php requests from working, I assume because the timthumb string includes ;q=21 (for example).

So, given that we only want to block requests that start with /?q=anytexthere how would we modify the RewriteCond ?

Help appreciated.
jammy-d0dgerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
What's wrong with:
RewriteCond %{QUERY_STRING} \bq=anytexthere\b [NC]
RewriteRule ^ - [F]

Open in new window


HTH,
Dan
0
jammy-d0dgerAuthor Commented:
Thanks Dan,

Well, as mentioned in the question, this unfortunately stops timthumb requests from working because timthumb requests also have a parameter in the querystring of "q=".   However, in timthumb the q= parameter is always at the end of the querystring, but the exploit we want to stop, always has just a q= parameter on it's own, straight after the '?'

i.e. http://mydomainname.com/about/?q=hot_online_poker

So, my original code snippet works and does block these exploit links being used but it also blocks timthumb requests such as:

/timthumb.php?src=http://mydomainname.com/wp-content/uploads/2015/04/my-special-image-980x2100.jpg&w=980&h=212&q=90

...because of the q=90 on the end.

Hope that makes sense.
0
Dan CraciunIT ConsultantCommented:
OK. Then how about any text not starting with a number?
RewriteCond %{QUERY_STRING} \bq=[a-zA-Z][0-9a-zA-Z]*$ [NC]
RewriteRule ^ - [F]

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

jammy-d0dgerAuthor Commented:
Ah Dan... that works a treat.  Can I include empty parameter in that too, i.e. ?q= with nothing after it, (currently this is not caught and get's through to the exploit).
0
Dan CraciunIT ConsultantCommented:
Use an OR:
RewriteCond %{QUERY_STRING} \bq=$ [NC,OR]
RewriteCond %{QUERY_STRING} \bq=[a-zA-Z][0-9a-zA-Z]*$ [NC]
RewriteRule ^ - [F]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jammy-d0dgerAuthor Commented:
And that right there, is a solution.  Many thanks Dan.
0
Dan CraciunIT ConsultantCommented:
You're welcome.

Glad I could help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.