title: AngularJs $routeProvider: using the redirectTo to redirect if a condition is met
body: <p>I have these routes:</p>
  
  <pre><code>$routeProvider.when('/events/agenda', {...});
  $routeProvider.when('/events/calendar', {...});
  $routeProvider.when('/events/:id', {...});
  </code></pre>
  
  <p>The "/events/:id" route should only be matched if ":id" is a number... Currently, urls like "/events/whateverilike" are matched by that route.</p>
  
  <p>So what I want to do is define my route as normal:</p>
  
  <pre><code>$routeProvider.when('/events/:id', { templateUrl: 'views/events/event-profile.html', controller: 'EventProfileCtrl',
      resolve: {
          event: function (EventLoader, $route) {
              return EventLoader({ id: $route.current.params.id });
          }
      });
  </code></pre>
  
  <p>but i want to make use of the redirectTo to validate the ":id" value. If its a number, continue to the view and controller, else redirect:</p>
  
  <pre><code>redirectTo: function(routeParams, path, search){
                          if (!+(routeParams.id)) // is :id a number?
                              return '/events/agenda';
  
                          return null;
                      }
  </code></pre>
  
  <p>but returning null doesn't negate the redirect.</p>
  
  <p>I really don't want to put this logic on the controller as I believe the "url-matching" and validating should occur before going to the controller and view. Am I understanding this correctly, does it make sense what I'm trying to do?</p>
  
  <p>Any help and suggestions would be much appreciated.</p>
  
  <p><strong>UPDATE:</strong>
  Having used the marked answer, I soon realized there was a problem with it if the url contained search params, ie: #/events/4450?join=true</p>
  
  <p>Using "return path;" then removed the search params. This should rather be:</p>
  
  <pre><code>redirectTo: function(routeParams, path, search){
      if (!+(routeParams.id)) // is :id a number?
          return '/events/agenda';
  }
  </code></pre>
  
  <p><strong>End Result:</strong></p>
  
  <pre><code>$routeProvider.when('/events/:id', { templateUrl: 'views/events/event-profile.html', controller: 'EventProfileCtrl',
      resolve: {
          event: function (EventLoader, $route) {
              return EventLoader({ id: $route.current.params.id });
          }
      },
      redirectTo: function(routeParams, path, search){
          if (!+(routeParams.id)) // is :id a number?
              return '/events/agenda';
      });
  </code></pre>