N
N
Nikita2015-09-02 08:47:56
JavaScript
Nikita, 2015-09-02 08:47:56

Duplicate pages in wordpress, how to fix it?

When visiting a page like domain.ru/page1/1, the page domain.ru/page1 is displayed.
The same thing happens when you score domain.ru/page1/123 , domain.ru/page1/123123 , domain.ru/page1/9999 (in general, when numbers are scored)
Tell me how to do this (at least where to look), what would be in such situation, a 404 error was displayed, or, in extreme cases, a redirect to the main one.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Sidorenko, 2015-09-08
@sidorenkoda

If you're worried about SEO, just write rel canonical and don't worry about what else will be added to your urals =)
seo-for-ucoz.com/blog/atribut_rel_canonical_chto_t...

T
TraiDeR, 2017-04-06
@TraiDeR

There is a solution to this problem. Here he gave the answer
In general, you just need to insert the code into the functions.php file of your theme:

global $posts, $numpages;

$request_uri = $_SERVER['REQUEST_URI'];

$result = preg_match('%\/(\d)+(\/)?$%', $request_uri, $matches);

$ordinal = $result ? intval($matches[1]) : FALSE;

if(is_numeric($ordinal)) {

    // a numbered page was requested: validate it
    // look-ahead: initialises the global $numpages

    setup_postdata($posts[0]); // yes, hack

    $redirect_to = ($ordinal < 2) ? '/': (($ordinal > $numpages) ? "/$numpages" : FALSE);

    if(is_string($redirect_to)) {

    // we got us a phantom
    $redirect_url = get_option('home') . preg_replace('%'.$matches[0].'%', $redirect_to, $request_uri);

    // if page = 0 or 1, redirect permanently
    if($ordinal < 2) {
        header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently');
    } else {
        header($_SERVER['SERVER_PROTOCOL'] . ' 302 Found');
    }

    header("Location: $redirect_url");
    exit();

    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question