REGEX – How to ignore some query strings in URLS, but not in others

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/

Read More

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!

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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;