NEW YORK (AP) — A Guantanamo Bay detainee awaiting trial in a federal civilian court cannot evade strip searches that require him to expose his private areas because security would be compromised, a judge has ruled.

U.S. District Judge Lewis A. Kaplan made the written ruling Monday and it was released publicly Thursday in the terrorism case brought against Ahmed Khalfan Ghailani, the first Guantanamo Bay detainee to be brought to a U.S. civilian court for trial.

Ghailani had asked Kaplan to force the Bureau of Prisons to spare him from a 1997 policy that requires all inmates entering or leaving the Metropolitan Correctional Center in Manhattan to submit to a visual inspection of "all body surfaces and body cavities."

A psychologist, Katherine Porterfield, had testified at a hearing several weeks ago that Ghailani did not object to the strip search so much as the requirement that he bend over or squat briefly to display his rectum to an officer conducting the search.

She said the search triggered Ghailani's post-traumatic stress disorder, which resulted from treatment he received during interrogations at a secret CIA-run camp overseas after his 2004 arrest.

Porterfield testified that "enhanced interrogation techniques" he experienced while in CIA custody caused him to have a severe reaction in anticipation of exposing his rectal area.

The judge sided with the government, which had argued that the prison regulation was reasonable and serves important governmental interests such as the protection of guards, court personnel and other inmates.

Kaplan noted that the policy was adopted nationally after it was realized that inmates will hide weapons or other illegal items in body cavities.

"This is a perfectly reasonable concern in light of both previous experiences with federal prisoners obtaining or fashioning weapons and common sense," the judge said. "Accommodating Ghailani's request by foregoing visual searches of the defendant's body cavities would risk the safety of guards, other inmates and court personnel by creating opportunities for the defendant to secrete weapons or contraband in his person."

Ghailani's lawyers had argued that their client cannot assist in his defense if he is traumatized by strip searches. Ghailani has skipped most of his scheduled court appearances since November.

Kaplan said he could be found incompetent to stand trial if he is unable to assist in his defense.

Ghailani was moved from the CIA camp to Guantanamo in Cuba in 2006. He is awaiting trial in the August 1998 bombings of two U.S. embassies in Africa. They killed 224 people, including a dozen Americans.

Lawyers for Ghailani did not immediately respond to a message for comment. Yusill Scribner, a spokeswoman for federal prosecutors, declined to comment.