Backport fix for #4889 from Yii 2 repository.

This is my suggested patch that fixes the redirect loop issue (issue #4889 from Yii 2 repository).