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?

+3


source to share


2 answers


You can use the regular expressions below:

/(.*(?<!friends)(?<!friends/))\?.*$

      

Watch the demo

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/

.

+1


source


Instead of replacing the first one, (.*)

you should simply add to it:

Source: /(?!friends/?)(.*)\?(.*)$
Target: /$1

      



The negative forward group (?!friends/?)

does not match itself; it just prevents certain matches.

0


source







All Articles