wordpress - WordPress - 你可以在页面上有斜杠,但在帖子中没有斜杠吗?
问题描述
一位朋友希望他的 WordPress 网站拥有:
- 页面看起来像目录(包括尾部斜杠 - www.domain.com/page/)
- 博客文章看起来像文件(.html 扩展名,没有斜线 - www.domain.com/post.html)
- 和产品看起来像文件( .html 扩展名 - www.domain.com/products/product.html)
默认情况下,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 扩展名的帖子并让页面保留尾部斜杠?
谢谢
解决方案
文件名不应以斜杠结尾。
如果您在文件名上强制使用斜杠,那么这将导致浏览器认为它是一个文件夹并导致 404 错误消息。 你应该在 URL 的末尾有一个斜杠吗?
不要忘记
如果您的页面可以使用或不使用尾部斜杠访问,它可能会导致重复内容和爬网效率问题。
了解有关特定页面名称的永久链接的更多信息
推荐阅读
- python - 在两个偏好列表中找到最佳匹配
- c# - 为什么 C# 线程不能在多个内核上工作?
- java - 如何使用私有成员Java创建具有子类的工作超类
- c# - 如何使用 caliburn micro 更好地控制动作冒泡?
- react-native - 尝试使用存储在两个组件中的链接连接到 api
- java - 从 Firebase 检索数据,但 snaptshop.get 返回 null
- c# - 如何将 JSON 作为原始 PostgreSQL 类型传递给使用 Dapper 的函数?
- android - Chaquopy 无法下载 NLTK 资源
- travis-ci - 将 .travis.yml 存储在 .travis 文件夹或其他位置
- python - 检测到 I2C 传感器但没有响应