url rewriting – Standard Regex syntax doesn’t work in WordPress rewrite rule

Question

I’ve run into a regex issue while adding a rewrite rule into a WordPress plugin. This blog has been helpful and the redirect works correctly after following this: https://brightminded.com/blog/mastering-wordpress-rewrite-rules/

However, I’m facing a challenge on this section:
enter image description here

That’s all fine, the redirect works perfectly. But I want to change the regex in the rewrite rule so that instead of matching city/{anything}/ it instead matches ‘city/?{anything}`. And I’m running into an issue:

The blog regex is invalid, yet it works

The regex in the blog is ^city/([^/]*)/? and this works perfectly in the wordpress rewrite rule, however when I paste this into any online regex tool such as regex101.com or regexr.com, they say that the regex isn’t valid:

enter image description here

Valid regex expressions do not work

I can easily create a regex that works correctly such as: ^city\/\?.* which matches to city/?{anything} just like I want. However, this regex doesn’t work in the WordPress rewrite rule. When I use this regex, and update permalinks in CMS, the redirect stops working.

So far, I can’t find any documentation for using regex in WordPress so I would presume it follows the de facto standard. But please can anyone shed light on this? Does wordpress use a different standard for Regex and where is the documentation for this? Or is there a simple syntax error that I’m missing?

0
5Diraptor 1 month 2023-02-23T11:58:22-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse