Alabama cannot prosecute doctors and reproductive health organizations for helping patients travel out of the state to obtain abortions, a federal judge ruled Monday.

Alabama has one of the strictest abortion bans in the country, and in 2022 its attorney general, Steve Marshall, a Republican, raised the possibility of charging doctors with criminal conspiracy for recommending abortion care out of state.

Multiple clinics and doctors challenged Marshall’s comments in court, accusing him of threatening their First Amendment rights, as well as the constitutional right to travel. The Justice Department under the Biden administration had also weighed in with support for the clinics, arguing that “threatened criminal prosecutions violate a bedrock principle of American constitutional law.”

On Monday, the judge, Myron H. Thompson of the Middle District of Alabama, in Montgomery, ruled that Marshall would be violating both the First Amendment and the right to travel if he sought prosecution.

“It is one thing for Alabama to outlaw by statute what happens in its own backyard,” Thompson, who was named to the court by President Jimmy Carter, wrote in his 131-page opinion.

“It is another thing,” he added, “for the state to enforce its values and laws, as chosen by the attorney general, outside its boundaries by punishing its citizens and others who help individuals travel to another state to engage in conduct that is lawful there but the attorney general finds to be contrary to Alabama’s values and laws.”

Thompson described a hypothetical scenario in which a bachelor party from Alabama could be prosecuted for casino-style gambling in Las Vegas, which is illegal in Alabama.

“As the adage goes, be careful what you pray for,” he wrote.

Travel to other states to obtain an abortion, or abortion pills, has significantly increased since the Supreme Court overturned Roe v. Wade. More than 171,000 patients traveled for an abortion in 2023, compared with 73,100 in 2019, according to the research organization Guttmacher Institute.

Marshall repeatedly defended his position in court, arguing that he retained the ability to prosecute a conspiracy that took place in Alabama and that the legality of abortion laws in other states did not matter. (He does not appear to have charged anyone in such a case.)

“The right to travel, to the extent that it is even implicated, does not grant plaintiffs the right to carry out a criminal conspiracy simply because they propose to do so by purchasing bus passes or driving cars,” Marshall wrote in one filing.

Republican-led states, like Alabama, generally have the most restrictive abortion laws in the country. Some of those states are now taking legal steps to stop out-of-state efforts to help residents obtain abortions.