【问题标题】:WordPress - Can you have Trailing Slashes for Pages but No Slashes for Posts?WordPress - 你可以在页面上有斜杠,但在帖子中没有斜杠吗?
【发布时间】:2021-01-20 19:43:37
【问题描述】:

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

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

我可以使用 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 扩展名的帖子并让页面保留尾部斜杠?

谢谢

【问题讨论】:

  • > 如果您在文件名上强制使用斜杠,那么这将导致浏览器认为它是一个文件夹并导致 404 错误消息。 Should You Have a Trailing Slash at the End of URLs?
  • 感谢您的回复。我已经有一个针对 404 问题的经过测试且有效的解决方案。但本着这个问题的精神,我想知道在 WordPress 中是否可以这样设置?

标签: wordpress .htaccess url permalinks


【解决方案1】:

文件名不应以斜杠结尾。

如果您在文件名上强制使用斜杠,那么这将导致浏览器认为它是一个文件夹并导致 404 错误消息。 Should You Have a Trailing Slash at the End of URLs?

别忘了

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

了解更多关于Permalink for specific page name - WordPress Development Stack Exchange

【讨论】:

    猜你喜欢
    • 2012-02-05
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2015-09-15
    • 2016-01-02
    • 2011-07-30
    相关资源
    最近更新 更多