2

I need to redirect an old blog URL to a new blog URL. The ID field is the key query string, and everything else in the query string should be ignored. The logic at a high level:

If old case insensitive URL matches: /Blog/Post.aspx? + ID=33 anywhere in the query string of the URL then I will redirect to: /newblog/newurl/

Current REGEX Code: (?i:/Blog/Post.aspx)|(\?)|(?i:id=33)

Success: /Blog/Post.aspx?id=33
Fails: /Blog/Post.aspx?ignore=me&id=33
Fails: /Blog/Post.aspx?ignore=me&id=33&ignoreme=too

How would I have it ignore the potential unknown query string ignore=me and ignoreme=too, but still come up with a REGEX match to redirect when the ID=33 is in the query string?

Thank you for the answer m.buettner!

2 Answers 2

2

Right now you would even redirect, if you have only ID=33 in your URL, or even if you have only a question mark in there. I suppose that is not what you want. You are probably looking for something like this:

(?i:/Blog/Post.aspx\?.*id=33(?!\w)).*

That will require /Blog/Post.aspx? and then allow arbitrary characters until the id=33 is encountered.

Depending on which language you are using this in, you could also use a lookahead, which makes it easier to check for different parameters, whose order you might not know:

(?i:/Blog/Post.aspx\?(?=.*id=33(?!\w))).*

This could then be easily extended to

(?i:/Blog/Post.aspx\?(?=.*id=33(?!\w))(?=.*another=requirement(?!\w))).*

With the first approach you would have to add two alternatives for both possible orders.

EDIT: A caveat for all three solutions: after the number they require a non-word character (that is anything but letters, digits or underscores). This means that they would give false positives in cases like ...id=33+34... and ...id=33%2F.... But these should not be generated by Wordpress in the first place.

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you :) I adjusted it to (?i:/Blog/Post.aspx\?.*id=33.*) to allow the ID to be in front, back, middle, or alone. However, how do I get it to fail on:/Blog/Post.aspx?id=330&ignore=me, which is currently a false positive match of ID=33 with querystring of id=330?
@JohnnyG77 If you don't use a regex method, that requires to match the entire string there is no need for the trailing .*. Where do you use this regex?
I'm using it for the URL redirection for this wordpress plugin: wordpress.org/extend/plugins/redirection. It only allows for one line regex statement, so I can't use more typical string manipulations.
@JohnnyG77 hm I am not sure if that needs to match the entire string, but it looks like it does. If you want to make my other too variants work in this case, simply add .* at the very end after the lookaheads.
Sadly, this does not match with: "(?i:/Blog/Post.aspx\?(?=.*id=33))"...it leaves out the trailing "&ignore=me". This does match: "(?i:/Blog/Post.aspx\?)|(.*id=33.*)", but gives me a false positive with ID 330. I have to add, I have no idea what query strings may show up (thanks google), so I don't know what else might be added in "ignore=me".
|
0

Ops, I was going to bring a general answer to match general attributes in an url! Well I'm gonna leave it here in case that you need it

DEMO

(?:(id|noignoreme|dontignoreme)=([^&\n]+)(?:\n|&|$))

With this you can add the parameters you want to accept and it will return it as group1 (the option) and group2 (the text of that option).

After that you could see if ID = 33 then do that; else do thot;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.