最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - SEF add_rewrite_rule & add_rewrite_tag not working

programmeradmin2浏览0评论

I'm making SEF urls for my plugin. To logic is: if there is my_shortcode on the page, then pass the rest of url as parametr dir= This code is perfectly works on one of my wordpress installations and not working on another. I got 404. What can be wrong on it?

add_action('init', array( $this, 'do_rewrite'  ));    
public function do_rewrite()
        {
            $page = get_page_by_path($_SERVER['REQUEST_URI']);
            if (! isset($page->ID)) {
                preg_match('#^/?(.*?)/(.+)/?$#', $_SERVER['REQUEST_URI'], $matches);
                $parent_page = get_page_by_path($matches[1]);
                if ($parent_page) {
                    $content = $parent_page->post_content;
                    if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {
                        add_rewrite_rule('^/?(.*?)/(.+)/?$', 'index.php?pagename=$matches[1]&dir=$matches[2]', 'top');
                        add_rewrite_tag('%dir%', '([^&]+)');
                    }
                }
            }
        }

P.s. redirect rules flushed

UPDATE: Anyone can explain me what's happening? I commented those 2 if's:

 // if ($parent_page) {

                       // if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {

and redirects starts working. Then I returned it back.. and it keeps working!

I'm making SEF urls for my plugin. To logic is: if there is my_shortcode on the page, then pass the rest of url as parametr dir= This code is perfectly works on one of my wordpress installations and not working on another. I got 404. What can be wrong on it?

add_action('init', array( $this, 'do_rewrite'  ));    
public function do_rewrite()
        {
            $page = get_page_by_path($_SERVER['REQUEST_URI']);
            if (! isset($page->ID)) {
                preg_match('#^/?(.*?)/(.+)/?$#', $_SERVER['REQUEST_URI'], $matches);
                $parent_page = get_page_by_path($matches[1]);
                if ($parent_page) {
                    $content = $parent_page->post_content;
                    if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {
                        add_rewrite_rule('^/?(.*?)/(.+)/?$', 'index.php?pagename=$matches[1]&dir=$matches[2]', 'top');
                        add_rewrite_tag('%dir%', '([^&]+)');
                    }
                }
            }
        }

P.s. redirect rules flushed

UPDATE: Anyone can explain me what's happening? I commented those 2 if's:

 // if ($parent_page) {

                       // if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {

and redirects starts working. Then I returned it back.. and it keeps working!

Share Improve this question edited Nov 9, 2020 at 13:22 Movs Ovs asked Nov 9, 2020 at 9:52 Movs OvsMovs Ovs 12 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Looks like the problem is flushing rules. Flushing rules from Settings-Permalinks doesn't help somehow, so I did it in code like this:

if (! in_array('index.php?pagename=$matches[1]&dir=$matches[2]', get_option('rewrite_rules'))) {
    flush_rewrite_rules();
}
发布评论

评论列表(0)

  1. 暂无评论