Stabilizing a terahertz quantum-cascade laser using near-infrared optical excitation