【发布时间】:2021-01-20 19:43:37
【问题描述】:
一位朋友希望他的 WordPress 网站拥有:
- 页面看起来像目录(包括尾部斜杠 - www.domain.com/page/)
- 博客帖子看起来像文件(.html 扩展名,没有斜线 - www.domain.com/post.html)
- 和产品看起来像文件(.html 扩展名 - www.domain.com/products/product.html)
默认情况下,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