Ad

How To Use SEO Friendly URLs In Backbone With PushState?

- 1 answer

I have a single-page app using Backbone.js and Marionette that currently uses fragment url syntax.

I'm working on modifying the app for SEO purposes to utilize pushState. While that will achieve part of my goal (removing the need for the hash bang), the other thing I am hoping to add to the app is more friendly urls.

For example, my app will display a product when you hit a url where 12345 is the ID of a product:

http://<myserver>/product/12345

I'm wondering if there is a way that my app can be aware of a more friendly url for this product like:

http://<myserver>/product/the-name-of-my-product

And internally it will do the same thing as hitting the URL that contains the ID of the product.

Is there a nice way to accomplish seo-friendly urls within a backbone app like that?

Ad

Answer

Since you said you want to use product names in url, I'm assuming they'll be unique (Otherwise the url can't be resolved properly).

In this scenario you should be able to use idAttribute option of the Backbone Model to instruct Backbone to consider the product name as the product id. Once you do that it should be creating url's using the name.

Another option is to use url option and provide a function that return the url you want (return something like root/ + model.name)

Ad
source: stackoverflow.com
Ad