technology and webhosting news

I was logged into my Google Search Console the other day and noticed that there were thousands of alerts regarding blocked pages all using the format /page/{number}. A spam site had linked to a bunch of non-existent paginated pages on our site and Google was wanting to index these.

In this guide I will show you how to remove paginated page URLs in WordPress, specifically for static pages.

Skip to the code snippet

What are Paginated URL’s & Do You Need Them?

A paginated URL is one that ends with the format /page/{number}/ such as /page/2/ and are intended for use when displaying threaded posts on the page. By default, WordPress automatically “creates” these pages for archives such as categories and for standard pages.

For archives, WordPress has “smart” code that is able to know how many paginated pages exist for the archive so if you were to try and access an archive using a URL such as /page/99999/ and you don’t have that many paginated pages it will return a 404 error.

Contrary, static pages don’t have any “smart” method for the paginated URL’s because it’s impossible for WordPress to know if that page even has pagination and much less how many paginated pages exist. A static page can also have multiple queries on it with different pagination.

So, if you were to access a standard WordPress page using the paginated URL format you may find that you can infinitely view the page using any the /page/ URL format and ANY number at the end. In this case, you don’t need these URL’s and for SEO reasons you should redirect them to the main page with a 301 redirect or return a 404 error.

When do You Need Paginated URLs?

You only need access to paginated URLs on pages that display posts with next and previous links or numbered pagination that refresh the page to display the next set of posts. Any page that displays static content without paginated posts does not need to have access to these paginated URLs.

If you are displaying paginated posts on standard pages, I would recommend using an AJAXED pagination such as a “load more” button. This way the user can view more posts without having to refresh the page. Our Total Theme’s Post Cards element has this functionality built in – awesome 😉

How to Remove Paginated (/page/) URLs from WordPress Pages

Now that you understand what paginated pages are and how they are used (if you didn’t before) I will show you how to remove these pages in WordPress. Ok, technically we aren’t “removing” the pages because they aren’t “real” pages, but what we are doing is preventing them from being accessed by redirecting them to the original page.

Here is the snippet to prevent paginated URLs from all standard pages:

/**
 * Redirect all paginated standard pages page-slug/page/{number}/ to the main page.
 *
 * @see https://www.wpexplorer.com/wpex_dev_tutorials/how-to-remove-paginated-page-urls-in-wordpress/
 */
add_action( 'template_redirect', function() {
	if ( is_page() && is_paged() ) {
		wp_safe_redirect( get_permalink( get_queried_object_id() ), 301 );
		exit;
	}
} );

What this code does is it checks to see if the page being requested is a standard page and paginated and if so it redirects it back to the main page without /page/ in the URL. It would also be possible to instead remove the WordPress /page/ rewrite rule (may be more efficient) but it’s much more complex and it won’t allow any page to be paginated.

Note of caution: If you use this code on your site, be sure to check all your site pages to ensure you were not using any sort of pagination on the page. For example on WPExplorer.com our theme and plugin collection pages display posts with pagination so if we added this code to our site it would break those pages.

How to Exclude Pages from the Code

If you have some pages that do require pagination you can modify the snippet above to exclude those pages so they can continue working as expected.

/**
 * Redirect some paginated pages.
 *
 * @see https://www.wpexplorer.com/wpex_dev_tutorials/how-to-remove-paginated-page-urls-in-wordpress/
 */
add_action( 'template_redirect', function() {
	$excluded_pages = [ 1, 2, 3, 4 ]; // array of page ID's to exclude
	if ( is_page() && is_paged() && ! in_array( get_queried_object_id(), $excluded_pages ) ) {
		wp_safe_redirect( get_permalink( get_queried_object_id() ), 301 );
		exit;
	}
} );

Simple modify the $excluded_pages array to be a comma separated list of the page ID’s you wish to exclude. All other pages that aren’t part of this variable will redirect if the user tries accessing the paginated version of that page.

Remove Paginated URLs from the Homepage Only

If you wish to remove the paginated URLs from the homepage only, first you will want to make sure your site is set up to use a static homepage. To use a static homepage log into your WordPress dashboard and go to Settings > Reading and choose a standard page for your homepage display.

From what I understand simply setting your static homepage should remove the paginated pages. However, I’ve noticed that this wasn’t the case on one of my sites. If you are having the same issue you can use the following code snippet:

/**
 * Redirect the paginated homepage pages back to the homepage.
 *
 * @see https://www.wpexplorer.com/wpex_dev_tutorials/how-to-remove-paginated-page-urls-in-wordpress/
 */
add_action( 'template_redirect', function() {
	if ( is_front_page() && is_page() && is_paged() ) {
		wp_safe_redirect( home_url( '/' ), 301 );
		exit;
	}
} );

Removing the Paginated URL’s is Easy But is it Necessary?

As you can see the code needed to remove (or more specifically redirect) the paginated URLs is minimal. But is it really necessary to add this code to your site? Honestly, I’m not sure if you should or shouldn’t use these code snippets.

Technically, if you don’t have links on the page to the paginated sub-pages Google or other crawl bots should never find them. However, if you previously had pagination on the page and removed it (perhaps you switched your homepage from an archive to a static page) or your site was backlink spammed (like ours) then Google may have indexed your paginated pages.

And perhaps having the unnecessary paginated pages could cause duplicate content issues (WordPress does add a canonical meta tag so this shouldn’t be a concern) or warnings in the Google Search Console you simply want removed. I guess it doesn’t hurt to remove the paginated pages since they aren’t needed…so why not?

If you have any insight or a better solution please share!

Similar Posts