简体   繁体   English

WordPress add_rewrite_rule返回404的字符串

[英]WordPress add_rewrite_rule for a string returning 404

I am currently working on a WordPress project and am a little stuck on the .htaccess rewrite rules. 我目前正在研究WordPress项目,并且对.htaccess重写规则有些困惑。

My WordPress rewrite rule code is as follows: 我的WordPress重写规则代码如下:

if (!function_exists('check_string_id_rule')){
function check_string_id_rule(){
    add_rewrite_rule('id-check/([^/]+)','id-check/?id=$1','top');
}
add_action('init', 'check_string_id_rule');

Disclaimer: according to the documentation I am supposed to use $matches[1] instead of $1. 免责声明:根据文档,我应该使用$ matches [1]代替$ 1。 I've tried that but, it will simply set the value of id to "matches[1]". 我已经尝试过了,但是它只会将id的值设置为“ matches [1]”。 $1 seems to do the trick, to an extent. 在一定程度上,$ 1似乎可以解决问题。

When I navigate to mywebsite.com/id-check/12 everything works fine and my id has the value of 12. 当我导航到mywebsite.com/id-check/12时,一切正常,我的id的值为12。

My ID's are however built up out of a number of characters containing letters from az, numbers from 0-9 and the occasional hyphen '-'. 但是,我的ID是由许多字符组成的,这些字符包含az的字母,0-9的数字以及偶尔的连字符'-'。

If I navigate to mywebsite.com/id-check/12a-5 , I get redirected to an empty page. 如果导航到mywebsite.com/id-check/12a-5 ,则会重定向到空白页面。 WordPress appears to redirect me to a page that doesn't exist. WordPress似乎将我重定向到了一个不存在的页面。 It is trying to redirect me to the /12a-5 page. 它试图将我重定向到/ 12a-5页面。 Going to mywebsite.com/id-check/?id=12-a5 works as intended. 转到mywebsite.com/id-check/?id=12-a5可以按预期工作。

Why is my my rule not being enforced? 为什么我的规则没有得到执行? I checked the .htaccess file and there don't seem to be any competing rules. 我检查了.htaccess文件,似乎没有任何竞争规则。

 <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^id-check/([^/]+) /id-check/?id=$1 [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Is this some WordPress quirk, or am I missing something here? 这是一些WordPress怪癖吗,还是我在这里错过了一些东西?

Thank you in advance, 先感谢您,

Stijn van der Pol 斯蒂金·范德波尔

I've managed to solve my own problem. 我设法解决了自己的问题。 It seems my issue resided in the second parameter of my add_rewrite_rule logic. 看来我的问题出在我的add_rewrite_rule逻辑的第二个参数中。

I partially followed the following article: http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ 我部分关注了以下文章: http : //www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/

I know hyperlinking is frowned upon, but credits to rlmseo.com. 我知道超链接不受欢迎,但要感谢rlmseo.com。 In short they suggested a different approach of registering rewrite rules. 简而言之,他们提出了另一种注册重写规则的方法。 With a couple of changes of my own it lead to the following code.. 经过我自己的一些更改,它导致了以下代码。

Within my functions.php: 在我的functions.php中:

if(!function_exists('add_rewrite_rules')){
    function add_rewrite_rules($aRules) {
        $aNewRules = ['id-check/([^/]+)' => 'index.php?pagename=id-check&id=$matches[1]'];
        $aRules = $aNewRules + $aRules;
        return $aRules;
    }
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');

And then I use the following tag rewrite logic in my functions.php as well: 然后在我的functions.php中也使用以下标记重写逻辑:

function id_rewrite_tag() {
    add_rewrite_tag('%id%', '([^/]+)');
}
add_action('init', 'id_rewrite_tag');

Then in my view I access the variable by using: 然后在我看来,我通过使用以下方式访问变量:

global $wp_query;
$id = $wp_query->query_vars['id'];

I hope this is of help to someone :-) 我希望这对某人有帮助:-)

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

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