简体   繁体   English

Wordpress重写规则

[英]Wordpress Rewrite Rules

UPDATE UPDATE

I've gone through the codex docs on the rewrite API and now have the following in my functions.php : 我已经阅读了重写API上的codex文档,现在我的functions.php中有以下内容:

function my_rewrite_rules() {
    add_rewrite_rule('(a|b|c|d)/?$', 'index.php?pagename=$matches[1]-overview&myVar=var', 'top');
}
add_action('init', 'my_rewrite_rules');

Yes, I am going to the permalinks page to flush the rules after adjusting. 是的,我会在调整后进入永久链接页面以刷新规则。 The behavior is the same, the rule above 404s even though the page does exist and I can access it by typing directly into the address bar. 行为是相同的,即使页面确实存在,404规则也是如此,我可以直接在地址栏中输入来访问它。 However, if I hardcode one of the regex matches like so: 但是,如果我像这样硬编码其中一个正则表达式匹配:

function my_rewrite_rules() {
    add_rewrite_rule('(a|b|c|d)/?$', 'index.php?pagename=a-overview&myVar=var', 'top');
}
add_action('init', 'my_rewrite_rules');

then all works as expected, with query vars set correctly. 然后所有工作都按预期工作,并正确设置查询变量。 Ideas? 想法?


ORIGINAL QUESTION 原始问题

I've been trying to get Wordpress rewrite rules to work for quite some time now and am absolutely stumped as to why the following code (in functions.php) doesn't work: 我一直试图让Wordpress重写规则工作很长一段时间,并且绝对难以理解为什么以下代码(在functions.php中)不起作用:

function my_rewrite_rules($rules) {
    $my_rules = array('(a|b|c|d)/?$' => 'index.php?pagename=$matches[1]-overview&my_var=somevar');

    return array_merge($my_rules, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');

I have canonical redirects disabled and the rewrite just 404s. 我禁用了规范重定向,重写只有404s。 If redirecting is enabled, it does go to the correct page, but my query variable is stripped. 如果启用了重定向,它会转到正确的页面,但我的查询变量被剥离。 If I remove '$matches[1]' and replace it with a, b, c, or d, everything works as expected with canonical redirecting disabled. 如果我删除'$ matches [1]'并将其替换为a,b,c或d,则一切都按预期工作,禁用规范重定向。 I realize there are a few workarounds but I just want to understand why the following doesn't work? 我意识到有一些解决方法,但我只是想了解为什么以下不起作用? Thanks! 谢谢!

Apparently having the $matches variable directly after the pagename query variable is treated as a special case in the url_to_post() function of Wordpress. 显然在页面名查询变量之后直接使用$ matches变量被视为Wordpress的url_to_post()函数中的特殊情况。 Here is a snippet from that code : 下面是从一个片断代码

if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
    // this is a verbose page match, lets check to be sure about it
    if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
        continue;
}

If I read this correctly it seems that Wordpress assumes (incorrectly) that the $matches variable should match the page path. 如果我正确读取它似乎Wordpress假定(错误地)$ matches变量应该与页面路径匹配。 So in your example, if you do not have a page with the name a , b , c or d your rewrite rule will be skipped entirely (continue will be called). 因此,在您的示例中,如果您没有名称为abcd的页面,则将完全跳过重写规则(将继续调用)。

I've deduced this from reading the Wordpress code, but I've not tested my theory (I've actually never worked with Wordpress at all). 我从阅读Wordpress代码中推断出这一点,但我没有测试过我的理论(我实际上从未使用过Wordpress)。 You could test my theory by making a pages with the names a , b , c , and d and running your code again. 您可以通过创建名称为abcd并再次运行代码来测试我的理论。 If I am correct this should make your rule work. 如果我是正确的,这应该使你的规则有效。 I would suggest not using -overview behind your pagenames, thus solving the problem. 我建议不要在页面-overview后面使用-overview ,从而解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM