Woocommerce pagination - prevent 301 redirect on page 1

Viewed 3211

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.

3 Answers

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);
});

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.

Related