Would it be OK to ask a professor who recommended me to take a specific PhD program to write recommendation letter for another PhD program? A professor recommended me to take PhD program A. However, I want to take PhD program B. Would it be still OK to ask this professor to write a recommendation letter for this PhD program B despite he recommended me to take PhD program A?

He would be an ideal professor to write recommendation for me.

(I am in Germany.)
Yes.
And any sane person would not hold this against you. It is also reality that people often have to apply to multiple places.
 (Also note that if you apply within Europe, you probably might upload the refeence letter yourself and can still decide to find another reference if the professor is insane.)

It could be different if the professor explicitly tells you not to apply to program B (but this does not seem to be the case from your question).