The cis-trans isomerization of azobenzene upon S1(n,π*) excitation is studied both in gas phase and in solution. Our study is based on ab initio non-adiabatic dynamics simulations with the non-adiabatic effects included via the fewest-switches surface hopping method with potential-energy surfaces and couplings determined on the fly. The non-adiabatic couplings have been computed based on overlaps of CASSCF wave functions. The solvent is described using classical molecular dynamics employing the quantum mechanics/molecular mechanics (QM/MM) approach. Azobenzene photoisomerization upon S1(n,π *) excitation occurs purely as a rotational motion of the central CNNC moiety. Two non-equivalent rotational pathways, corresponding to clockwise or counterclockwise rotation, are available. The course of the rotational motion is strongly dependent on the initial conditions. The internal conversion occurs via a S0/S1 crossing seam located near the midpoint of both of these rotational pathways. Based on statistical analysis it is shown that the occurrence of one or other pathways can be completely controlled by selecting adequate initial conditions. The effect of the solvent on the reaction mechanism is small. The lifetime of the S1 state is marginally lowered; the effect does not depend on the polarity, but rather on the viscosity of the solvent. The quantum yield is solvent dependent; the simulations in water give smaller quantum yield than those obtained in n-hexane and in gas phase.