Add rewrite rule in plugin: with .htaccess in plugin folder or using WordPress functions

NOTE: WordPress Rewrite API is not the same as Apache Rewrite module.
WP Rewrite API doesn’t redirect a request to another URL, it used to
parse current URL and fill query_vars array.

The issue is in the second parameter of you add_rewrite_rule function call. It has to start from index.php? and then there should be your arguments, like pid, for example:

"index.php?pid=$matches[1]...."

So your add_init function should be like this:

add_action( 'init', 'wpse8170_add_init' );
function wpse8170_add_init()
{
    add_rewrite_rule('my-plugin/pages/tp(.*)\.php', 'index.php?pid=$matches[1]', 'top');
}

Don’t forget to flush rewrite rules by visiting Settings ยป Permalinks page.

Further reading:

Leave a Comment