Set URL to SEO Friendly Title with Dashes instead of ID Set URL to SEO Friendly Title with Dashes instead of ID angularjs angularjs

Set URL to SEO Friendly Title with Dashes instead of ID


I would recommend to add anther route in addition to the existing route

  when('/phones/:phoneId', {    templateUrl: 'partials/phone-detail.html',    controller: 'PhoneDetailCtrl'  }).  when('/phones/:phoneId/:title', {    templateUrl: 'partials/phone-detail.html',    controller: 'PhoneDetailCtrl'  })

For your case, you can have the following links which goes to the same page.

 <a href="http://my.url.com/articles/#66D5069C-DC67-46FC-8A51-1F15A94216D4">Start Investing</a> <a href="http://my.url.com/articles/#66D5069C-DC67-46FC-8A51-1F15A94216D4/Start+Investing">Start Investing</a>

The second will work the exactly the same as the first one


You can use one of the following options, I have ordered them by my preference:

  1. Just use the formatted title in url and don't add anything else unless there is a collision between titles. In case of collision in title then use one of the methods below to distinguish between them. Your route would look like '/articles/:formattedTitle'. If there are multiple articles in database with same title and there is no explicit indicators of which one to pick then go ahead and pick the one created earliest.
  2. Create a new field called textKey, and auto populate that field uniquely based on the title of the article. e.g. one will result in http://my.url.com/articles/start-investing and the next one results in http://my.url.com/articles/start-investing-2. Your route would look like '/articles/:textKey'
  3. Create a new field called discriminator with default value of Null only populate it only if collision occcurs in titles. e.g. one will result in http://my.url.com/articles/start-investing and the next one results in http://my.url.com/articles/start-investing/1. Your route would look like '/articles/:formattedTitle/:discriminator'
  4. add both ID and title e.g. one will result in http://my.url.com/articles/start-investing/66D5069C-DC67-46FC-8A51-1F15A94216D4 and the next one results in http://my.url.com/articles/start-investing/AAE5069C-ABCD-46FC-8A51-1F15A94213A5. Your route would look like '/articles/:formattedTitle/:id'


I would suggest that you use the author's username as well in the URL , so that the route will look like

.when('/article/:userID/:articletitle', {     templateUrl: './views/articles.html',     controller: 'articleCtrl'   })

This would solve the problem of different author having same article title (but now there would be a problem if the author publishes two articles with the same title. For this you can either introduce the year or month in the URL to make it unique)

Also you can create another view where author can see all their articles (route would be like)

.when('/article/:userID', {     templateUrl: './views/allArticles.html',     controller: 'allArticleCtrl'   })