WASHINGTON — The Senate gave final approval on Wednesday to legislation that strengthens the policing of sex trafficking, over the opposition of many internet companies. Lawmakers are trying to catch up to the reality of prostitution long after the bartering of children and adults moved from the streets to the web.

The 97-to-2 vote was the culmination of a multiyear effort by Republicans and Democrats to allow state law enforcement officials to go after websites like Backpage.com that facilitate sex trafficking. The bill would also suspend protections that shielded internet companies from legal liability for the content on their sites.

The legislation has pitted lawmakers against Silicon Valley companies and civil liberties groups, which hold starkly differing views on the government’s oversight of the internet. Big tech companies like Facebook and Google have flourished with little regulation for years, but they have come under intense scrutiny after their platforms were manipulated by foreign agents during the 2016 presidential election.

With passage of the Fight Online Sex Trafficking Act, Silicon Valley’s ability to stay out of the government’s reach suffered a rare setback. The bill passed the House overwhelmingly last month, and President Trump is expected to sign it into law.