• Resolved pnsfakos

    (@pnsfakos)


    Hello,

    I change my old permalinks from

    /%postname%/%post_id%/

    to

    /%category%/%postname%/%post_id%/

    But i can’t find the the correct regex to redirect the links! Can you help please?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author John Godley

    (@johnny5)

    It’s not possible to use a regex for that as category is not in the original URL.

    Instead you may be able to use the beta permalink migration feature: https://redirection.me/support/site-options/

    Thread Starter pnsfakos

    (@pnsfakos)

    Thanks John!

    I try the beta permalink migration but nothing changed!

    I gave this /%postname%/%post_id%/ on the field and then click save!

    I clear the cache also not working on any URL still getting 404 error.

    Plugin Author John Godley

    (@johnny5)

    Did you clear your browser cache?

    As it states in the guide, it is in beta and doesn’t support every permalink.

    Thread Starter pnsfakos

    (@pnsfakos)

    Found solution playing little with PHP.

    Puting the following code to functions.php you get redirection from /%postname%/%post_id%/ to /%category%/%postname%/%post_id%/

    add_action( 'parse_request', 'maybe_redirect_old_permalinks' );
    function maybe_redirect_old_permalinks( $wp ) {
        if ( preg_match( '#^([^/]+)/(\d+)$#', $wp->request, $matches ) ) {
            list ( $path, $slug, $id ) = $matches;
            // Redirect to the new permalink, if the slug and ID match.
            if ( $slug === get_post_field( 'post_name', $id ) ) {
                wp_redirect( get_permalink( $id ), 301 );
                exit;
            }
        }
    } 
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Permalink – Cant find right regex’ is closed to new replies.