0

I am learning how to use WordPress rewrites. As a start i am just trying to redirect any request for "validate_data.php" to "validate-data". Here is what i have added to the functions.php file:

function custom_rewrite_basic() {
  add_rewrite_rule( 'validate_data.php', 'validate-data', 'top' );
}
add_action( 'init', 'custom_rewrite_basic' );

I know i can execute this in the .htaccess file, but i am testing it here as i have a more advanced case, so it is just a start. What happens when i access "validate_data.php" is that it causes 404 error. I hope someone can tell me what i am doing wrong here?

1

I am new to WordPress rewrites, so after some StackExchange reading i have figured it out. All rewrites should be passed to index.php, then i can call the page that i want to redirect to, like this:

function custom_rewrite_basic() {
    add_rewrite_rule( 'validate_data.php', 'index.php?pagename=validate-data', 'top' );
}
add_action( 'init', 'custom_rewrite_basic' );

In addition i wanted to pass some POST data from the old page to the new page, so i did it like this:

function custom_rewrite_basic() {
    add_rewrite_rule( '^validate_data.php(.+)/?$', 'index.php?pagename=validate-data', 'top' );
}
add_action( 'init', 'custom_rewrite_basic' );

Hope that helps new starters like me some day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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