Your two primary goals are to maintain your head above water and, wherever possible, to maneuver yourself away from the river's flow.
You may be stuck in a current moving away from the shore if you are getting exhausted while attempting to swim toward the beach but do not appear to progress.
When you find yourself stuck in a rip current, the best advice anyone can give you is to swim parallel to the beach until you can free yourself.
According to the National Oceanic and Atmospheric Association, the general strategy should be to stay alongside the shore but swim perpendicular to the current as much as possible "at an angle away from the current and towards the shore." That isn't incorrect, but it's essential to know that many rip currents come in at an angle. The general strategy should be to stay alongside the shore but swim as much as possible perpendicular to the wind.
It's possible that swimming in one way rather than the other will be less difficult. In addition, evidence suggests that certain currents move in a cyclical pattern, initially moving away from the beach and then making their way back. Because of this, several scholars have hypothesized that rather than fighting against the river, it could be preferable to attempt to float with it.