0

Newbie WordPress developer here.

I need to map a WordPress URL like this www.example.com/user/Username to a search result based on that Username

I already have the page that makes the search with the url www.example.com/search-results/?name=Username

In the functions.php I have

add_action( 'init', 'add_username_rules' );
function add_username_rules()
{
    global $wp_rewrite;

    add_rewrite_rule(
    "user/([^/]*)",
    "search-result/?name=$matches[0]",
    "top"
    );

    $wp_rewrite->flush_rules();
}

But this is not working and the rule is going to non_wp_rules.

1 Answer 1

1

That's going to the non wp rules because you're not mapping the URL to the index.php page format.

If you want it to be a WP rule, then you need to start the destination with index.php and include the query string to define what you want the query to contain.

So if you wanted it to go to a specific Page, for example, then you could set it as:

index.php?page=123&name=$matches[0]

WordPress rewrite rules map the "pretty" url formats into the "default" url formats only. They're not full implementations of the htaccess rewrite system.

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.