I am having an issue with redirect rules for a single-page app that is on a sub-page of a WordPress site. I have followed this set of instructions pretty directly and am still having issues: Redirect sub-pages to parent without changing URL
The subpages are custom post types for business locations. When someone visits http://business.com/hollywood-ca/contact
it should pull up http://business.com/hollywood-ca/
but the url needs to remain the same (The contact portion of the URL is part of a single-page Vue.js app on each location page, so it needs to stick around). Here is my code:
//This function hooks in on post save
function add_location_deep_links( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "location" != $post_type ) return; // If this isn't a 'location' post, don't update it.
$slug = $post->post_name; //hollywood-ca
add_rewrite_rule(
"^{$slug}/[A-Za-z]", //regex prevents trying to redirect the /hollywood-ca/ page
"index.php?page_id={$post_id}", //redirect to post
'top' // take precedence over other redirects
);
flush_rewrite_rules();
}
The problem is when I visit http://business.com/hollywood-ca/contact
the page redirects to http://business.com/hollywood-ca/
which prevents my single-page app from navigating to the contact tab.
If it helps, I have also written a couple of functions that change my URLs from business.com/location/hollywood-ca
to the cleaner business.com/hollywood-ca
. I have tested these issues without those functions and am still having issues, so I don’t think they are related.