Redirect everything except routes to "special" controller


I am trying to build an URL shortner as simple as possible. I have come with the idea of two controllers:

  • SiteController with a number of actions – for managing the service
  • DefaultController with just a single index (default) action – for actual redirection

How should I configure urlManager component / write it’s rules so that:

  • any route to SiteController (+any error, internal Yii routes etc.) would be served normally,
  • any other route would be redirected to the other controller’s default action

In addition routes to the second controller must be hidden from URL, so:


How to build urlManager rules to achieve the above? Thank you.

Please check if this is of help

Thank you! Certainly worth taking closer look at.

The first solution (with pooling DB in app’s configuration) sounds scary. The second one (with onBeforeAction method in default countroller) sounds much more promising.

I was also thinking about “brute-force” mode, meaning intercepting site/error action for the default controller:

  • checking, if error code is 404,
  • preventing default execution,
  • checking URL vs. DB there and
  • throwing 404 only, if no token for URL short is found in DB.

What do you think about this?

With onBeforeAction solution, you can easily set your own logic as you wish!