plugins – I custom url form post wordpress

I have customized the post url link in wordpress, domain.com/post >> domain.com/cate/post
but I want it to change the path to the new link.
What do I do?

function aly_post_redirects() {

    if ( ! is_user_logged_in() )
        return;

    if ( is_404() ) :

        $slug   = $_SERVER('REQUEST_URI');
        $slug   = str_replace( '/', '', $slug );

        $item   = get_page_by_path( $slug, OBJECT, 'post' );

        if ( ! $item )
            return;

        $post_link  = get_permalink( $item->ID );

        if ( ! $post_link )
            return;

        wp_redirect( esc_url_raw( $post_link ), 301 );
        exit;

    endif;

}

add_action ( 'template_redirect', 'aly_post_redirects', 1 );

function wpse147453_remove_child_categories_from_permalinks( $category ) {
    while ( $category->parent ) {
        $category = get_term( $category->parent, 'category' );
    }

    return $category;
}

add_filter( 'post_link_category', 'wpse147453_remove_child_categories_from_permalinks' );