Support » Developing with WordPress » blog pagination throwing 404 after adding custom permalink and rewrite rules

  • Naresh Kumar

    (@naresh-kumar-1)


    I added the below script in my website to set up a custom language slug like th, se etc.

    After adding this below script my pages are working like.

    http://www.mywebsite.com/
    http://www.mywebsite.com/se/
    http://www.mywebsite.com/th/

    Issue: but my blog pagination like throwing 404 error.

    http://www.mywebsite.com/blog/page/2
    http://www.mywebsite.com/se/blog/page/2
    http://www.mywebsite.com/th/blog/page/2

    Script custom permalink and rewrite rules:

        /*---------------Page permalinks ---------------*/
            add_filter( 'page_link', 'prefix_custom_link_option', 10, 3 );
            
            function prefix_custom_link_option($link, $post_id , $sample){
            	global $wp_rewrite;
                $category = get_the_terms($post_id, "nk-post-translation");
            	$post = get_post($post_id);
            	if (!empty($category) && $category[0]->slug == "th") {
            	    $link = str_replace(home_url(),home_url('th'), $link);
            	} elseif (!empty($category) && $category[0]->slug == "se") {
            		$link = str_replace(home_url(),home_url('se'), $link);
            	} else {
            		$link = str_replace(home_url(),home_url(), $link);
            	}
            	return $link;
            }
            
            
            /*-----------------page rewrite rule -----------*/
            add_filter('page_rewrite_rules', 'my_new_page_rewrite_rules');
            function my_new_page_rewrite_rules($page_rewrite) {
            	$rules['^th/(.*)'] = 'index.php?&pagename=$matches[1]'; //for thai pages
            	$rules['^se/(.*)'] = 'index.php?&pagename=$matches[1]';  //for swedish pages
            	$rules['^(.*)'] = 'index.php?&pagename=$matches[1]';   //for default or english pages
            	return $rules;
            }
            
            
            add_filter('post_link', 'custom_permalink', 10, 3);
            add_filter('post_rewrite_rules', 'wp_insertMyRewriteRules');
            
            //rewrite_rules_array
            
            add_filter('init', 'flushRules');
            
            // creating of post permalink from taxonomy slug
            function custom_permalink($permalink, $post, $leavename) {
            	$category = get_the_terms($post->ID, "nk-post-translation");
            	if (!empty($category) && $category[0]->slug == "th") {
            	    $permalink = str_replace(home_url(),home_url('th'), $permalink);
            	} elseif (!empty($category) && $category[0]->slug == "se") {
            		$permalink = str_replace(home_url(),home_url('se'), $permalink);
            	} else {
            		$permalink = str_replace(home_url(),home_url(), $permalink);
            	}
            	return $permalink;
            }
            
            //flush the existing rules of taxonomy slug rules
            function flushRules() {
            	global $wp_rewrite;
            	$wp_rewrite->flush_rules();
            }
            
            // inserting new rules of taxonomy slug
            function wp_insertMyRewriteRules($post_rewrite) {
            	$post_type = 'post';
            	$rules['^th/(.*)$'] = 'index.php?post_type=' . $post_type . '&post=$matches[1]&name=$matches[1]'; 
            	$rules['^se/(.*)$'] = 'index.php?post_type=' . $post_type . '&post=$matches[1]&name=$matches[1]'; 
            	$rules['^(.*)$'] = 'index.php?post_type=' . $post_type . '&post=$matches[1]&name=$matches[1]'; 
            	return $rules;
            }
    • This topic was modified 1 day, 3 hours ago by Naresh Kumar.
    • This topic was modified 1 day, 3 hours ago by Naresh Kumar.
    • This topic was modified 22 hours, 14 minutes ago by bcworkz.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Your rules like $rules['^(.*)'] = 'index.php?&pagename=$matches[1]'; are assigning the entire “blog/page/2” as the requested page slug instead of just “blog”. A page named like that does not exist. Your regex needs to capture individual link elements instead of the entire thing. You’ll need separate rules for a non-paged request (i.e. /blog) and another for paged requests where the matched page number is set as the “paged” query var.

    Thread Starter Naresh Kumar

    (@naresh-kumar-1)

    @bcworkz thank you for your reply.
    may I get your working suggestion with my script edited?
    I made a try but I am not getting what to change. I am learning this rewriting.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.