Conditional redirect to URL doesn't work properly