WordPress: How To 301 Redirect Pagination Pages To The Guardian URL


It could be anecdotal, however I’m discovering that Google Search Console is turning into extra complete in its reporting. A lot in order that I’ve begun to lean on it increasingly more… particularly to establish pages not discovered on the positioning which may negatively influence search rankings.

My web site’s theme incorporates an method the place guests can click on load extra in the event that they’d prefer to see extra posts for the house web page, class archives, tag archives, and even search outcomes. This person expertise (UX) enhancement does away with pagination, the place a person (or search crawler) would iterate by pages and weblog publish outcomes.

Nonetheless, regardless of enabling this performance on my web site, Google nonetheless believes that I’ve pagination and that there are errors on the positioning the place these pages are not discovered. If I went to a kind of pages listed in Google Search Console, it resulted in a 404. Google believes there’s an issue with my web site when there isn’t.

To treatment this, I wanted to construct a customized redirect in my little one theme features.php that takes any of these /pages/# and redirects them to their dad or mum. One exception that I wanted was my customized publish sort for my acronyms.

In the present day, I pushed this enhancement to my little one theme which accommodates any of the situations above:

  • /web page/{quantity} – My residence web page is my weblog archive, so this could lead to a 404 with my load extra characteristic. Now any web page will redirect to my residence web page.
  • /tag/{tag-slug}/web page/{quantity} – My tag pages would lead to a 404 with my load characteristic. Now any web page will redirect to dad or mum /tag/{tag-slug}/
  • /class/{category-slug}/web page/{quantity} – My class pages would lead to a 404 with my load characteristic. Now any class web page will redirect to dad or mum /class/{category-slug}/
  • /{category-slug}/web page/{quantity} – My class pages strip out /class/ and would lead to a 404 with my load characteristic. Now any class web page will redirect to dad or mum /{category-slug}/

Nonetheless, my acronym pages are nonetheless indexable:

  • /acronym/web page/{quantity} nonetheless exists and might be crawled and listed.

Baby Theme Code

add_action('init', 'custom_redirect_with_exceptions');
perform custom_redirect_with_exceptions() {
    // Common expression to match paths ending with /web page/quantity
    if (preg_match('/^(/[^/]+)/web page/d+/', $_SERVER['REQUEST_URI'], $matches)) {
        $base_path = $matches[1]; // Seize the bottom path
        // Record of base paths to exclude from redirection (e.g., customized publish sorts)
        $exclusions = ['/acronyms']; // Change along with your precise base paths

        // Examine if the bottom path isn't within the exclusions listing
        if (!in_array($base_path, $exclusions)) {
            // Examine for tag or common slug
            if (preg_match('/^/tag/', $_SERVER['REQUEST_URI'])) {
                // Particular logic for tag redirection: take away '/tag' prefix
                $redirect_base = str_replace('/tag', '', $base_path);
            } else {
                // Normal slug logic: use the bottom path as is
                $redirect_base = $base_path;
            // Assemble the brand new URL to redirect to the bottom path with out pagination
            $new_url = home_url($redirect_base);
            // Redirect to the brand new URL with a 301 everlasting redirect standing
            wp_redirect($new_url, 301);
        // No motion is taken if the URL is within the exclusions listing
    // Examine if the URL is only a paginated residence URL
    else if (preg_match('/^/web page/d+/', $_SERVER['REQUEST_URI'])) {
        // Redirect paginated residence URLs to the house web page
        wp_redirect(home_url("https://martech.zone/"), 301);

The code first checks if the URL matches a paginated format excluding specified paths. If it does, and it’s not an excluded path, it redirects to a non-paginated model of that path. If the URL is a paginated residence URL, it redirects to the house web page. Clarification:

  • add_action('init', 'custom_redirect_with_exceptions');: Hooks the perform custom_redirect_with_exceptions into WordPress’s initialization course of.
  • perform custom_redirect_with_exceptions() { ... }: Defines the perform that can deal with the redirection logic.
  • Contained in the perform:
  • preg_match('/^(/[^/]+)/web page/d+/', $_SERVER['REQUEST_URI'], $matches): Makes use of a daily expression to verify if the present URL follows the sample of a base path adopted by /web page/ and a quantity (e.g., /category-name/web page/2). If it does, the bottom path is captured and saved in $matches[1].
  • $base_path = $matches[1];: Shops the captured base path from the URL into the variable $base_path.
  • $exclusions = ['/acronyms'];: Defines an array of base paths that shouldn’t be redirected in the event that they match the present URL’s base path.
  • if (!in_array($base_path, $exclusions)) { ... }: Checks if the captured base path isn’t within the listing of exclusions.
    • Inside this conditional:
    • if (preg_match('/^/tag/', $_SERVER['REQUEST_URI'])) { ... }: Determines if the URL begins with /tag/, indicating it’s a tag URL.
      • If true: $redirect_base = str_replace('/tag', '', $base_path);: Removes /tag from the bottom path, getting ready it for redirection to the tag’s important web page.
      • Else: $redirect_base = $base_path;: If it’s not a tag URL, makes use of the bottom path as is for redirection.
    • $new_url = home_url($redirect_base);: Constructs the brand new URL to redirect to, eradicating the pagination however preserving the bottom path.
    • wp_redirect($new_url, 301);: Redirects the person to the constructed URL with a 301 (Moved Completely) standing code.
    • exit;: Stops additional processing to make sure the redirection takes impact instantly.
  • else if (preg_match('/^/web page/d+/', $_SERVER['REQUEST_URI'])) { ... }: Checks if the URL is a paginated residence URL format, like /web page/2.
    • Inside this conditional:
    • wp_redirect(home_url("https://martech.zone/"), 301);: Redirects paginated residence URLs again to the house web page.
    • exit;: Stops additional processing, guaranteeing the redirection is executed.


Please enter your comment!
Please enter your name here