Solved

Regex to Match Anything but the Number 23, isapai rewrite

Posted on 2014-03-06
11
325 Views
Last Modified: 2014-03-07
Hello

I am trying to write a regular expression like the following to redirect query string id to 23 if it receives anything but 23:

RewriteRule ^manual-treadmill.aspx\?ID=[^23]+.* manual-treadmill.aspx?ID=23 [NC,O,R=301]

It works great unless they put a number that starts with a two or three. So it is basically breaking if they put:

2
3
32
233

but it works if they put
1
4
5
6
7
8
9
23
11
41
51
61
71
81
91
etc.

Can someone please tell me how to update this only redirects if they don't put 23 in that order?

Thanks in advance,
Shawn
0
Comment
Question by:smower
  • 6
  • 5
11 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
If you want to redirect query string id to 23 if it receives anything but 23, why not redirect everything to 23 and use RewriteCond to skip the rule if it is already 23?
0
 

Author Comment

by:smower
Comment Utility
Not sure how to do that. I have very limited understanding of ISAPI rewrite. I have a slight knowledge of regular expressions but I was trying to figure out how to do the regular expression to find any query ID except for 23.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
if your regular expression engine does not support (?!) then you might use (.?$|[^2][^3]|...)
0
 

Author Comment

by:smower
Comment Utility
Thanks, that fixes the few example numbers but is still not quite getting everything for example
123
2333
-123
322

I need it to capture everything except 23 only.

Thank you,
Shawn
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I don't know why 123 and 322 would not match, but if the expression is anchored, you may have to change ... to ...+
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:smower
Comment Utility
^manual-treadmill.aspx\?ID=(.?$|[^2][^3]|...+)

it's still not working for me. I am testing it on:
http://www.regexr.com/
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
In what way is it not working?  It seems to do what I expect.

By the way http://www.regexr.com/ seems to support (?!23)
0
 

Author Comment

by:smower
Comment Utility
it only partially matches. For example:
123 or 1234 or 12345  or 123456 or '123 or 123" or -123 or 123- etc. - only catches the first couple numbers or characters.
33 doesn't get captured
22 doesn't get captured
anyway these are just a couple of examples. I need it to capture everything except 23 exactly.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
Sorry, I thought you only needed a match, not a capture.
You can make the ...+ the first option:
(...+|.?$|[^2][^3])
Or, since http://www.regexr.com/ supports (!?)
(?!23$)(.*)
0
 

Author Comment

by:smower
Comment Utility
Awesome, thank you so much.  This one worked!: (?!23$)(.*)

the other one mostly worked: (...+|.?$|[^2][^3])
However it would still have issues with:
33
20
21
22
24
25
26
27
28
29

but since the other one works I will just use that.
Thank you!
Shawn
0
 

Author Closing Comment

by:smower
Comment Utility
(?!23$)(.*)  works.
(...+|.?$|[^2][^3]) partially works.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now