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!
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:That will require
/Blog/Post.aspx?
and then allow arbitrary characters until theid=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:
This could then be easily extended to
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.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;