I'm trying to create a product compare feature where we can compare three products, Two products must be added while the third is optional.
Trying to use the add_rewrite_rule to create a friendly URL for the product compare page. For example, Something similar to this https://hostadvice.com/tools/web-hosting-comparison/godaddy-vs-hostinger/
To make it work, The following is the what i tried so far, I know this is not the correct one.
add_rewrite_rule(
'compare/([^-vs-]*)(?:-vs-)([^-vs-]*)(?:-vs-)([^/]*)?/?',
'index.php?pagename=compare&compare=$matches[1],$matches[2],$matches[3]',
'top'
);
desired url structure:
example.com/compare/product1-vs-product2-vs-product3
While the product3
can be optional
It'd be really helpful if someone can correct me on how to make the regex expression work correctly.
Thank you.