I managed to solve the problem. Here’s how to do it, in case anyone else ever needs this solution.
** Updated Mar 10 3:30am EST **
The previous function that I had here was displaying the page properly, but was returning a 404 error code. I’ve replaced the bad code example with a working one that does not return a 404.
function check_section_page( $posts ) {
if (is_admin()) { return $posts; }
global $wp, $wp_query;
$requested_path = $wp->request;
if ( $requested_path == "" ) { return $posts; }
preg_match("/^([a-z\-]+)\//", $requested_path, $matches);
if ( count($matches) > 1 ) {
if ( get_page_by_path( $matches[1] ) ) { $section = $matches[1]; }
}
$wp_query->set("saleslink_section", $section);
$this_page = get_page_by_path( $requested_path );
$new_requested_path = preg_replace("/^(.*?)\//", "", $requested_path);
$requested_page = get_page_by_path($new_requested_path);
if ( $requested_page->ID and ! $this_page->ID ) {
$posts = NULL;
$posts[] = $requested_page;
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->is_home = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
unset($wp_query->query["error"]);
$wp_query->query_vars["error"]="";
$wp_query->is_404=false;
}
return $posts;
}
remove_filter('template_redirect','redirect_canonical');
add_filter("the_posts", "check_section_page");
The remove_filter
line will turn off the automatic-url-guessing-redirect behaviour of WordPress, when a requested URL does not exist and a similar one does and it redirects to the similar URL.