If I use the simple code below:

function my_custom_rewrite_rules() {
    add_rewrite_rule(
        '^rewriteme$',
        'index.php?page_id=1',
        'top'
    );
}
add_action('init', 'my_custom_rewrite_rules');

I would expect to be able to visit http://example.com/rewriteme and see the content of the Hello World post while keeping /rewriteme in the address bar. It doesn’t work, though. It actually redirects to http://example.com/hello-world/.

How do I get it to display the content of the Hello World post without actually changing the URL in the address bar?

(And yes, I’ve flushed rewrite rules.)

1 Answer
1

If you just want to see the content of the /hello-world post on /rewriteme page you can try to add something like this on the page.php of your theme:

<?php
    if (is_page('rewriteme')) {
        // query for the about page
        $your_query = new WP_Query( 'postname=hello-world' );
        // "loop" through query (even though it's just one page) 
        while ( $your_query->have_posts() ) : $your_query->the_post();
            the_content();
        endwhile;
        // reset post data (important!)
        wp_reset_postdata();
    }
?>

Leave a Reply

Your email address will not be published. Required fields are marked *