Second thought - beforeAction is probably invoked too late for your needs. I think that better is to override the errorAction (usually \yii\web\ErrorAction) and there you call for example Yii::$app->getRequest()->getPathInfo() to get the url user entered.