2

I have a rewrite rule in my functions.php which is working just fine.

function custom_offers_rewrite() {
  add_rewrite_rule('^offers/([a-z-_]+)/?', 'index.php?page_id=1948&offer_restaurant=$matches[1]', 'top');
}

This looks for anything like /offers/my-custom-path and then loads in a specific page template / ID (page_id=1948). This template displays some custom things and we're all good.

Whilst that's fine, I have a literal page on the URL path, /offers/all and I would like to exclude this from the rewrite rule.

Is this possible at all?

So, in other words, if we hit /offers/all, just ignore the rule and carry on as normal. If anything else, do the rule as it currently works.

I suppose it needs some adjustment to the Regex, but I'm barely any good at pattern matching.

Help and advice appreciated. Many thanks!

1 Answer 1

5

Okay, I figured it out eventually.

Working code that will ignore /offers/all but work with everything else:

function custom_offers_rewrite() {
  add_rewrite_rule('^offers/(?!all)([a-z-_]+)/?', 'index.php?page_id=1948&offer_restaurant=$matches[1]', 'top');
}

The key part, being the addition of (?!all) into the Regex.

Hope this manages to help someone else.

Important: Remember to save the permalinks in WordPress in order to flush the rewrite cache!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.