将特定自定义post类型的URL仅从尾部斜杠重定向为非尾部斜杠,可以通过以下步骤实现:
function redirect_custom_post_type() {
global $wp_rewrite;
$custom_post_types = array('your_custom_post_type'); // 替换为你的自定义post类型
foreach ($custom_post_types as $post_type) {
$rule = '^' . $post_type . '/([^/]+)/?$';
$rewrite = 'index.php?post_type=' . $post_type . '&name=' . $wp_rewrite->preg_index(1);
add_rewrite_rule($rule, $rewrite, 'top');
}
}
add_action('init', 'redirect_custom_post_type');
function redirect_trailing_slash($url, $status) {
if (is_singular('your_custom_post_type') && substr($url, -1) == '/') {
wp_redirect(rtrim($url, '/'), $status);
exit();
}
return $url;
}
add_filter('redirect_canonical', 'redirect_trailing_slash', 10, 2);
请注意,上述代码中的"your_custom_post_type"需要替换为你的自定义post类型的名称。
这样,当访问特定自定义post类型的URL时,如果URL以尾部斜杠结尾,用户将被重定向到相同的URL,但尾部斜杠将被移除。
这种重定向可以提供更好的URL一致性和用户体验,同时也有助于避免搜索引擎将同一内容视为重复内容的问题。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云