kb.
kb.

Reputation: 1060

Woocommerce pagination - prevent 301 redirect on page 1

Using latest versions of Wordpress & Woocommerce.

If I have 2 pages of products in my shop & I click PAGE 1 in my pagination links, I get a 301 redirect that goes to /shop.

For SEO purposes I'd like to disable the 301 redirect & keep the link as it is in the pagination links i.e /shop/page/1

This seems to be the default behaviour in Woocommerce & it's not something my Shopkeeper theme is doing.

Upvotes: 3

Views: 5256

Answers (3)

Christopher Geary
Christopher Geary

Reputation: 715

You can use the following regex to match only page 1. The answer by SaRiD will also match pages 10 - 19, 100+ etc as they all start with 1.

add_filter('paginate_links', function ($link) {
    return preg_replace('#page/1[^\d\w]#', '', $link);
});

Upvotes: 2

SaRiD
SaRiD

Reputation: 1050

Had the same issue with a site I'm building. Although you're asking to keep /shop/page/1/ (with no 301) it is actually better to simply have /shop/ as your page one. Otherwise you have two identical pages serving up the same content which Google is not fond of.

The approach I took was to change the output of the pagination to NOT include /page/1/ in the link. This then removes the 301 as the link is correct to start with. To do this I used the filter paginate_links.

add_filter('paginate_links', function($link) {
    $pos = strpos($link, 'page/1/');
    if($pos !== false) {
        $link = substr($link, 0, $pos);
    }
    return $link;
});

Hope this helps.

Upvotes: 9

jMike
jMike

Reputation: 312

The answer by SaRiD will match all page numbers starting with a 1.

The answer by Christopher Geary uses preg_replace, which is unnecessary and should be avoided if possible.

If you don't need fancy replacing rules (like regular expressions), you should use this function instead of preg_replace().

The following solution works with str_replace and only! if the trailing slash in WordPress permalinks is not disabled and there is no further path part /page/1/ in permalinks.

add_filter( 'paginate_links', function( $link ) {
    return str_replace( '/page/1/', '/', $link );
});

Thanks to SaRiD and Christopher Geary for your previous answers.

Upvotes: 1

Related Questions