WordPress: Display A Post At A URL without Redirecting?

Joined
Dec 2, 2014
Messages
246
Likes
476
Degree
1
I want to have a single post show up for multiple URLs. The post would be at "category/post-name/" and I want all URLs like "category/post-name/asdfa" to show the post, but keep the URL not redirect to the post.

So far, using the rewrite_rule and template stuff, I can only get it to redirect to the post, not stay at the same URL and show the post.

<?php
/*
Plugin Name: Custom URL Handler
Description: Allows appending any string to a specific page URL without causing an error.
Version: 1.0
Author: Your Name
*/

// Hook into 'init' action to add custom rewrite rules
add_action('init', 'custom_url_handler_rewrite_rules');

function custom_url_handler_rewrite_rules() {
add_rewrite_rule(
'^viral/post-name/.*',
'index.php?pagename=viral/post-name',
'top'
);
flush_rewrite_rules(false);
}

// Flush rewrite rules on plugin activation
register_activation_hook(__FILE__, 'custom_url_handler_activate');
function custom_url_handler_activate() {
custom_url_handler_rewrite_rules();
flush_rewrite_rules();
}

// Flush rewrite rules on plugin deactivation
register_deactivation_hook(__FILE__, 'custom_url_handler_deactivate');
function custom_url_handler_deactivate() {
flush_rewrite_rules();
}
?>

This is code to work for a single post just as a test but it doesn't work and just redirects. I've tried a few other things already but nothing is working, no other plugins active and default theme.
 
Back