【发布时间】:2017-02-24 06:54:00
【问题描述】:
我在 Wordpress 网站子页面上的单页应用程序的重定向规则方面遇到问题。我非常直接地遵循了这组说明,但仍然遇到问题:https://wordpress.stackexchange.com/questions/193479/redirect-sub-pages-to-parent-without-changing-url
子页面是针对营业地点的自定义帖子类型。当有人访问http://business.com/hollywood-ca/contact 时,它应该拉起http://business.com/hollywood-ca/,但 URL 需要保持不变(URL 的联系人部分是每个位置页面上单页 Vue.js 应用程序的一部分,因此它需要保留)。这是我的代码:
//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();
}
问题是当我访问http://business.com/hollywood-ca/contact 时,页面会重定向到http://business.com/hollywood-ca/,这会阻止我的单页应用导航到联系人选项卡。
如果有帮助,我还编写了几个函数,将我的 URL 从 business.com/location/hollywood-ca 更改为更清洁的 business.com/hollywood-ca。我已经在没有这些功能的情况下测试了这些问题,但仍然有问题,所以我认为它们不相关。
【问题讨论】:
标签: wordpress redirect url-rewriting single-page-application vue.js