简体   繁体   English

WordPress Rewrite_Rules和Rewrite_Tags PHP函数的正则表达式问题

[英]Regex Issue with WordPress Rewrite_Rules and Rewrite_Tags PHP Functions

I am not a great regex mind, but I feel like that is the problem with my code here, and wonder if someone can lend better eyes. 我不是一个很好的正则表达式专家,但是我觉得这就是我的代码存在的问题,想知道是否有人可以提供更好的支持。

The code is this bit of PHP... 代码就是PHP的这一部分...

function add_results_rewrite_tags() {
add_rewrite_tag('%resultant%','([^0-9]+$)');
}
add_action( 'init', 'add_results_rewrite_tags' );

function add_results_rewrite_rules() {
add_rewrite_rule('^([^&]+)/your-result/([^0-9]+$)/?$','index.php?    
name=$matches[1]&resultant=$matches[2]','top');
}
add_action( 'init', 'add_results_rewrite_rules' );

What I expect to happen is that a URL like http://deja1987shadow.wpengine.com/test-post/your-result/five/ will result in a 404 not found error, while a URL formatted as http://deja1987shadow.wpengine.com/test-post/your-result/5/ will return a found page. 我希望发生的是,像http://deja1987shadow.wpengine.com/test-post/your-result/five/这样的URL会导致404找不到错误,而URL格式为http:// deja1987shadow。 wpengine.com/test-post/your-result/5/将返回找到的页面。

However, I actually get the opposite results here... URLs ending with /five/ (or other text) will come up with a 'hit', while the numeric endings like /5/ will come up 404! 但是,我在这里实际上得到了相反的结果...以/ five /(或其他文本)结尾的URL将带有“ hit”,而像/ 5 /这样的数字结尾将带有404!

It seems to me like I must be doing my regex patterns wrong, but I can't see why I'm so wrong as this. 在我看来,我一定在做我的正则表达式模式错误,但是我不明白为什么我这么错。 It's possible I'm also using these wordpress functions wrong somehow, but I feel like I've got that part figured out well enough...clearly I don't get a 404 when some URLs contain /your-result/ in them, which is really what these functions are meant to do. 可能我也在某种程度上错误地使用了这些wordpress函数,但我觉得我已经弄清楚了这一部分...显然,当某些URL包含/ your-result /时,我没有得到404,这些功能实际上就是要执行的操作。 Now I just want to make sure that only numeric values of "resultant" result in hits. 现在,我只想确保只有“结果”的数值会导致匹配。

Any ideas? 有任何想法吗? Thanks! 谢谢!

I don't think the first part of your regex will match the post url. 我认为您的正则表达式的第一部分与帖子网址不匹配。 I'm assuming the first match is alphanumeric. 我假设第一场比赛是字母数字。 Also, this is an Apache RewriteRule so there won't be a $matches variable, just a regex placeholder. 另外,这是一个Apache RewriteRule,因此不会有$ matches变量,只有正则表达式占位符。

function add_results_rewrite_rules() {
    add_rewrite_rule(
        '^([A-za-z\-]+)/your-result/([0-9]+$)/?$',
        'index.php?name=$1&resultant=$2',
        'top'
    );
}

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

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