The heat transfer and flow structure in rotating Rayleigh-Bénard convection are strongly influenced by the Rayleigh (Ra), Prandtl (Pr), and Rossby (Ro) numbers. For Pr≳1 and intermediate rotation rates, the heat transfer is increased compared to the nonrotating case. We find that the regime of increased heat transfer is subdivided into low- and high-Ra-number regimes. For Ra≲5×108 the heat transfer at a given Ra and Pr is highest at an optimal rotation rate, at which the thicknesses of the viscous and thermal boundary layers are about equal. From the scaling relations of the thermal and viscous boundary layer thicknesses, we derive that the optimal rotation rate scales as 1/Roopt≈0.12Pr1/2Ra1/6. In the low-Ra regime the heat transfer is similar in a periodic domain and cylindrical cells with different aspect ratios, i.e., the ratio of diameter to height. This is consistent with the view that the vertically aligned vortices are the dominant flow structure. For Ra≳5×108 the above scaling for the optimal rotation rate does not hold anymore. It turns out that in the high-Ra regime, the flow structures at the optimal rotation rate are very different than for lower Ra. Surprisingly, the heat transfer in the high-Ra regime differs significantly for a periodic domain and cylindrical cells with different aspect ratios, which originates from the sidewall boundary layer dynamics and the corresponding secondary circulation.