I need Wordpress to redirect requests matching a specific url structure to another script than index.php.

I'm using nginx so i cannot simply add a rewrite rule in the .htaccess file nor do I have access to the server configuration file. So I guess my only option is to do it from within WordPress.

I tried the following, then saved the Settings>Permalinks to flush the rewrite rules, but it does not work.

function rewrite_action() {
    add_rewrite_tag('%template%', '([^/]*)');
    add_rewrite_tag('%title%', '([^/]*)');
    add_rewrite_rule( '^partners/([^/]*)/?', 'partners-output.php?template=$matches[1]', 'top');
    add_rewrite_rule( '^partners/([^/]*)/([^/]*)$?', 'partners-output.php?template=$matches[1]&title=$matches[2]', 'top');

add_action( 'init', __NAMESPACE__ . '\rewrite_action', 1, 0 );

Right now, it only returns WordPress's 404 page.

1 Answer 1


Normally, to add rewrite rules in Wordpress there is the generate_rewrite_rules filter. From there you can call the function add_external_rule ($wp_rewrite->add_external_rule($rule)) where you could add your rule which not maps to index.php. Unfortunately Wordpress will try to write this rule into the .htaccess file. But maybe this could be helpful.

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.