简体   繁体   English

WordPress add_rewrite_rule 404错误

[英]WordPress add_rewrite_rule 404 error

I am adding rewrite_rules for my custom project. 我正在为自定义项目添加rewrite_rules。

I was able to make them work till some extent, but have some difficulties. 我能够使它们工作到一定程度,但是有一些困难。

Here's what i have tried out so far 到目前为止,这是我尝试过的

public function add_query_vars($vars) {
   $vars[] = 'gallery';
   $vars[] = 'project';
   $vars[] = 'palbum';
   return $vars;
}
public function add_endpoint() {
   add_rewrite_rule('^gallery/([^/]*)/([^/]*)?','index.php?gallery=true&project=$matches[1]&palbum=$matches[2]','top');
}

Here project is parent and palbum are all its child. 在这里,项目是父母,而纸浆都是孩子。

It seems to work properly at mysite.com/gallery/my-project-name/my-album-name (These page is to show single album) 它似乎在mysite.com/gallery/my-project-name/my-album-name正常工作(这些页面显示单个专辑)

But it gives me 404 error at mysite.com/gallery/my-project-name (These page is to show all albums which the project is parent of) 但这在mysite.com/gallery/my-project-name给了我404错误(这些页面将显示该项目所在的所有相册)

Any clue why? 有什么线索吗?

Based on your rewrite rule, both the section used for project and palbum are required in order for the rewrite rule to apply. 根据您的重写规则,必须使用用于project和palbum的部分,才能应用重写规则。 If you modify the rule just slightly, it will match what you are looking for: 如果您稍微修改规则,它将与您要查找的内容匹配:

public function add_endpoint() {
    /**
     *
     * Matches:
     * http://example.com/gallery/my-project-name/my-album-name/
     * http://example.com/gallery/my-project-name/my-album-name
     * http://example.com/gallery/my-project-name/
     * http://example.com/gallery/my-project-name
     */
    add_rewrite_rule('^gallery/([^/]+)/?(.*?)/?$','index.php?gallery=true&project=$matches[1]&palbum=$matches[2]','top');
}

Created a test to run against any other iterations you'd like to check - https://www.regex101.com/r/f54odP/1 创建了一个测试以针对您要检查的任何其他迭代运行-https: //www.regex101.com/r/f54odP/1

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

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