Regex matches a url that does not start with a line in WP Redirection plugin
I am currently using the Redirection plugin in WordPress to redirect all urls containing q question mark like this:
Source: /(.*)\?(.*)$
Target: /$1
It works well. It redirects any link with ?
, for example /good-friends-are-great.html?param=x
to /good-friends-are-great.html
.
However, now I need to make an exception. I need to be allowed to /friends
pass GET parameters, for example. /friends?guest=1&event=chill_out&submit=2
OR /friends/?more_params
, no parameter truncation.
I tried to change the regex in the plugin:
Source: /(?!friends/?)\?(.*)$
Target: /$1
But it didn't work. With the above expression, any link with is ?
no longer redirected.
You can help?
source to share
You can use the regular expressions below:
/(.*(?<!friends)(?<!friends/))\?.*$
The regex uses 2 negative appearances because in this regex we cannot use variable width hovering. (.*(?<!friends)(?<!friends/))
matches any number of characters before ?
, but checks if it follows ?
either friends
or friends/
.
EDIT:
Here's my first regex, which doesn't work very well for the current scenario:
/((?:(?!friends/?).)+)\?.*$
Its subpattern (?:(?!friends/?).)+
matches a string that does not contain friends
or friends/
.
source to share