0

We have our blog posts on a custom page with a custom pagination. For example a blog post name is url.de/postname instead of url.de/blog/postname.

Therefore we have a page /blog/ which has the pagination on it. The links from the pagination module like /blog/2/ lead to a 404 error page, because the /page/ part is missing.

I cannot edit the page urls - therefore i would like to use a redirect rule for this.

What i got till now:

function my_pagination_rewrite() {
    add_rewrite_rule( '(([^/]+/)*blog)/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[3]', 'top' );
}

add_action( 'init', 'my_pagination_rewrite' );

But this leads to /blog/2/page/2 --> 404.

How can i get this code to return /blog/page/2 or just return /blog/2/ (and show the content of /blog/page/2)

Thanks for help!

2 Answers 2

0

I'll add another answer because the rule is only a part of the problem.

Here is what I have tested and is working :

<?php
class BadRewritesRules {

    public function __construct() {
        add_action('init', [$this, 'custom_post_types_rewrite_rules']);
        remove_filter('template_redirect', 'redirect_canonical');
    }

    public function custom_post_types_rewrite_rules() {
        add_rewrite_rule('(blog)/([0-9-]+)[/]?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
    }

}

new BadRewritesRules();

In reality, I used another URL and you can see it working here : https://tests.pierre-roels.com/bad-rewrite-rules/2/ You can use pagination and also view each "member" with the same slug.

But as I said, this is a poor solution in my opinion because we must remove the template_redirect action hook. It would be better to hook on the Laravel part to build links the right way :( Or maybe an .htaccess redirect ?

Don't forget to flush permalinks everytime you make a change. To do so, you can simply go to Settings > Permalinks and submit the page

Here is the solution with .htacess. Add this to your .htaccess :

RewriteRule (blog)/([0-9]+)/? /$1/page/$2/ [R=301,L]

and don't forget to fix you rule : add_rewrite_rule('(blog)/page/([0-9-]+)[/]?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');.

I have made another test just for you : https://tests.pierre-roels.com/blog/2/

This solution allow you to not remove the template_redirect hook, it redirects the user the right end-url

0
-2

You should keep the slug page in your URL. It is way more easier and better to read.

Here is something correct :

add_rewrite_rule( '(blog)/page/([0-9-]+)[/]?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top' );
6
  • The slug /page/ is not present. That is part of the problem. I want to add it. ?!
    – Max
    Aug 25, 2021 at 11:45
  • I just read that you you use a custom pagination. So you need to add the "page" part in your links. Why don't you show your custom pagination code instead ?
    – Pierre R
    Aug 26, 2021 at 5:54
  • As i already said: i cannot change this! This comes from Laravel. I am looking for a different solution.
    – Max
    Aug 26, 2021 at 6:45
  • There is too many missing information to help you out. How can you not change links ? How this is Laravel in Wordpress code and no hooks ? Why is there a redirection from your link URL to another URL that is more like a WP one ? Anyway, you can start by rewrite you rule because you use $matches[1] and in your group it captures the first slash (/). According to my answer, you should start by using (blog) in your rule
    – Pierre R
    Aug 26, 2021 at 7:04
  • The information is pretty clear: add_rewrite_rule for /blog/2 --> /blog/page/2
    – Max
    Aug 26, 2021 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.