California did not violate freedom of religion or the right to an education when it required virtually all public and private school students to be vaccinated against contagious illnesses in 2016, a state appeals court says.

“Compulsory immunization has long been recognized as the gold standard for preventing the spread of contagious diseases,” the Second District Court of Appeal in Los Angeles said Monday. Citing California rulings as early as 1890 that rejected challenges to mandatory-vaccination laws, the court said the new law was not discriminatory and was a valid measure to protect public health.

The law was prompted by an outbreak of measles in 2014 that was traced to youngsters at Disneyland who had not been vaccinated. It requires all schoolchildren to be inoculated against illnesses including measles, mumps, chickenpox, tetanus, whooping cough and rubella.

The law repealed a personal-belief exemption that parents could claim to keep their children from being vaccinated. The only students who can go without vaccines now are children with doctor-certified medical exemptions and those who are schooled at home.

However, the law requires parents to provide immunization records only when a student is entering kindergarten or the seventh grade. As a consequence, an elementary-school student with a previous parental exemption doesn’t need to be vaccinated until the seventh grade, and students who were in the eighth grade or higher when the law took effect in July 2016 and had a past exemption will not have to be vaccinated.

A federal judge in San Diego refused to block enforcement of the law in August 2016, and the plaintiffs in that case decided not to appeal. Monday’s ruling, the first by a state appellate court on the issue, came in a suit by six students represented by advocates who claimed that vaccines kill people and that the law was a “totalitarian mandate.”

The plaintiffs’ lawyer, T. Matthew Phillips, could not be reached for comment on the ruling. In a statement when the suit was filed, Phillips said he was fighting for “our constitutional right to live vaccine-free.”

The court, however, said there was solid scientific evidence that compulsory vaccination prevents outbreaks of diseases, and that “community immunity” is reduced when large numbers of children are exempted, leading to the reappearance of illnesses such as measles.

Quoting the U.S. Supreme Court in a 1944 ruling that refused to exempt a religious group from child-labor laws, the state court said, “The right to practice religion freely does not include the liberty to expose the community or the child to communicable disease or the latter to ill health or death.”

Although the right to an education is part of the state Constitution, it is not absolute and must give way when necessary to the state’s need to protect children’s health, Justice Elizabeth Grimes said in the court’s 3-0 ruling. She also rejected the plaintiffs’ argument that they were being unwillingly subjected to a “medical experiment.”

State Sen. Richard Pan, D-Sacramento, author of the vaccination law, praised the court for “rejecting unproven assertions and accepting the scientific evidence.”

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter: @BobEgelko