2

I want to rewrite an url and pass the whole path as a parameter. Something like:

www.example.com/csearch/foo1/bar1/foo2/foo3/bar4 => www.example.com/index.php?page_id=23&csearch=foo1/bar1/foo2/foo3/bar4

The script on page_id 23 will parse the content of csearch.

The path after /csearch/ varies in length. It can have 0 or 30 levels.

I have a working solution for only one level

www.example.com/csearch/foo1 => www.example.com/index.php?page_id=23&filter=foo1

works fine with the following added to functions.php

function create_new_url_querystring() {
    add_rewrite_rule('^csearch/([a-zA-Z-]*)/?$', 'index.php?page_id=23&filter=$matches[1]', 'top');
}
add_action('init', 'create_new_url_querystring');

I believe I need a new regex to match the entire path, not only the first level.

Any idea on how to solve my problem?

Thanks.

1 Answer 1

4

A quick tweak to your regular expression. You are currently capturing only letters before the first /

add_rewrite_rule('^csearch/(.*)$', 'index.php?page_id=23&filter=$matches[1]', 'top');

Note when parsing this that you may or may not have a trailing slash at the end of your value of filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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