Redirect no product url’s to static url

If you prefer more control in coding, you may use

to build a checking when WordPress query is being setup. The advantage of code against .htaccess is that it is relatively server independent such as server migration, site migration and so on.

The following code is placed in theme functions.php and proved to work in a testing site.

add_filter( 'request', 'ws365986_check_request' );
function ws365986_check_request( $query ) {
    // var_dump($_SERVER['REQUEST_URI']); // for debug

    // only check for product url if /product/ is found in URL
    if( isset( $_SERVER['REQUEST_URI'] ) && preg_match( '/\/product\//', $_SERVER['REQUEST_URI'], $matches ) ) {
        // check if url product exist
        if( empty( url_to_postid( $_SERVER['REQUEST_URI'] ) ) ) {
            // redirect if return is 0 (empty)

            $url = home_url( '/sample-page' );
            // wp_redirect( $url ); // because $url is prepared by internally, wp_redirect should be enough
            wp_safe_redirect( $url );
            exit(); // exit script after redirect
        }
    }

    // default
    return $query;
}

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş