我在这里读过很多帖子,但我不明白重写规则是如何工作的。
我需要重写简单的目录路径:
http://example.com/category/plugin-reviews/
:
http://example.com/plugin-reviews/
对于这些,我使用了下一个代码:
function add_rules_for_rewrite() {
add_rewrite_rule(
'/plugin-reviews/?$',
'index.php?category_name=plugin-reviews',
'top');
flush_rewrite_rules();
}
add_action( 'init', 'add_rules_for_rewrite' );
在某一页我正在检查规则:
global $wp_rewrite;
print_r( $wp_rewrite->rules );
我看到了我的规则:
[/plugin-reviews/?$] => index.php?category_name=plugin-reviews
为什么不工作?非常感谢你的解释!
这将使你的重定向规则以你想要的方式工作,只有当永久链接设置设置为默认
function add_rules_for_rewrite() {
add_rewrite_rule(
'plugin-reviews/?$',
'index.php?category_name=plugin-reviews',
'top');
}
add_action( 'init', 'add_rules_for_rewrite' );
我认为你不需要这行代码:
flush_rewrite_rules();
add_rewrite_rule函数的创建就是为了让你能够在Wordpress中管理重定向规则。用最简单的方式来解释,下面是rewriteRule的工作原理
RewriteRule & lt;字符串匹配> <重定向url><重定向和优先级选项>重定向和优先级选项>重定向url>
这是一篇关于。htaccess中重写规则的基本用法的文章:http://kb.mediatemple.net/questions/85/Using+.htaccess+rewrite+rules#gs
如果不需要使用Wordpress函数来管理重定向规则那么我建议你使用。htaccess,因为它更方便但如果你正在开发一个插件或主题,需要经常管理重定向规则那么你应该使用Wordpress函数