How to create a redirect to another domain like safe redirect manager from php

By looking over the code of Safe Redirect Manager, we were able to find what we were looking for. We just needed to create an instance of the redirect post type using code similar to this:

$post_args = array(
    'post_type'   => 'redirect_rule',
    'post_status' => $sanitized_post_status,
    'post_author' => 1,
    'menu_order'  => $sanitized_menu_order,
);

$post_id = wp_insert_post( $post_args );

if ( 0 >= $post_id ) {
    return new WP_Error( 'error-creating', esc_html__( 'An error occurred creating the redirect.', 'safe-redirect-manager' ) );
}

// update the posts meta info
update_post_meta( $post_id, '_redirect_rule_from', wp_slash( $sanitized_redirect_from ) );
update_post_meta( $post_id, '_redirect_rule_to', $sanitized_redirect_to );
update_post_meta( $post_id, '_redirect_rule_status_code', $sanitized_status_code );
update_post_meta( $post_id, '_redirect_rule_from_regex', $sanitized_enable_regex );