首页 > 解决方案 > WordPress - 你可以在页面上有斜杠,但在帖子中没有斜杠吗?

问题描述

一位朋友希望他的 WordPress 网站拥有:

默认情况下,WordPress 将为页面和帖子添加斜杠。将自定义永久链接结构设置为/%postname%.html会将扩展名添加到blog posts,但当然会从页面中删除尾部斜杠。

我可以使用 add_permastruct 重写产品后期类型

function rr_permastruct_html( $post_type, $args ) {
    // Works fine!
    if ( $post_type === 'product' )
        add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%.html", $args->rewrite );
    
    // Worth a shot, but no...   
    //if ( $post_type === 'page' )
    //    add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%/", $args->rewrite );
}

但是,当涉及到附加带有 .html 扩展名的帖子并且仍然有页面保留尾部斜杠时,我一直在碰壁。

我无法使用 .htaccess 让它工作,因为它会影响两者。一段时间后,我对 $wp_rewrite 的尝试无济于事:

function rr_rewrite_page_permalink() {
    global $wp_rewrite;
    if (!strpos($wp_rewrite->get_page_permastruct(), '.html')) {
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
        $wp_rewrite->set_permalink_structure( $wp_rewrite->page_structure );
    }
    $wp_rewrite->flush_rules();
}
add_action('init', 'rr_rewrite_page_permalink', -1);

永久链接插件会产生相同的行为,但没有解决方案。那么,是否有可能发布带有 .html 扩展名的帖子并让页面保留尾部斜杠?

谢谢

标签: wordpress.htaccessurlpermalinks

解决方案


文件名不应以斜杠结尾。

如果您在文件名上强制使用斜杠,那么这将导致浏览器认为它是一个文件夹并导致 404 错误消息。 你应该在 URL 的末尾有一个斜杠吗?

不要忘记

如果您的页面可以使用或不使用尾部斜杠访问,它可能会导致重复内容和爬网效率问题。

了解有关特定页面名称的永久链接的更多信息


推荐阅读