PHP script to redirect traffic based on website accessed

I’am not absolutely sure what you mean but probably something like this may help you:

if( $_SERVER[ 'SERVER_NAME' ] == 'website1.com' ):

    header( 'Location: https://www.example.com/w1' ); exit;

elseif( $_SERVER[ 'SERVER_NAME' ] == 'website2.com' ):

    header( 'Location: https://www.example.com/w2' ); exit;

endif;