0

I am creating php pagination and I want to matching the /page/1 in URL "/product-category/womens-jackets/page/1" If the /page/1 in matched then redirect it to the main URL "/product-category/womens-jackets" because I don't want duplicate page issue. If it is page 1 it should redirect to main URL.

Also there is RegEx rules If the page is on main URL so the rule will be below.

$product_category_rules = "/product-category/(?'shop'[\w\-]+)";

And if the URL is like "/product-category/womens-jackets/page/2" not 1 so the rule will be, For pagination.

$product_category_rules = "/product-category/(?'shop'[^/]+)/page/(?'page'\d+)";

I am just confused with the code how to conditionalize it with below condition. Also you can help me with if available any other best method to figure it out.

if(preg_match('/\/page\/[1]+\/?$/', $uri, $m))
{
    // Remove the /page/1 from url and redirect
    $redirect = preg_replace('/\/page\/[1]+\/?$/', '$1', $uri);
    header('Location: '.$root.$redirect);
    exit();
}
else
{
 
}
2
  • I redirecting the page/1 to main URL so the page/1 will be not duplicate. I just fix the code right now but I just confused it is the right way or the code is good or garbage? Commented Nov 23, 2023 at 12:52
  • 2
    The proper solution to prevent duplicate content issues is not redirecting, but to specify the correct URL for any piece of content via en.wikipedia.org/wiki/Canonical_link_element Commented Nov 23, 2023 at 13:01

1 Answer 1

0

I just fixed the issue with match the only /page/ in URL in 2nd conduction.

// REDIRECT PAGINATION TO PAGE IF PAGINATION IS PAGE 1
if(preg_match('/\/page\/[1]+\/?$/', $uri, $m))
{
    // Remove the /page/1 from url and redirect
    $redirect = preg_replace('/\/page\/[1]+\/?$/', '$1', $uri);
    header('Location: '.$root.$redirect);
    exit();
}
elseif(preg_match('/\/page\/?/', $uri, $m)) // If matched /page/ only apply this rule.
{
    // For pagination add page/1234
    $product_category_rules = "/product-category/(?'shop'[^/]+)/page/(?'page'\d+)";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.