How to redirect to a new URL based on conditions?