skip to Main Content

I wanna make my urls SEO friendly.
I have seen a lot of articles and other posts on this subject, but all of them ends up with urls like: /Products/34/my-handbag

I wanna end up with urls like: /gucci/my-handbag

I already have the controls to actually get the product from the names “gucci” and “my-handbag”, i just need my routing to send me to Products controller.

This is what I am working with right now:

routes.MapRoute(
    name: "ProductDetails",
    url: "{brand}/{title}",
    defaults: new { controller = "ProductViews", action = "Details", brand = "", title = "" }

Any suggestions?

2

Answers


  1. You’re on the right path. You can generate an url like:
    http://www.domain.com/gucci/some-gucci-product-name by means of the following route.

    routes.MapRoute(
     "Default", // Route name
     "{Brand}/{Details}", // URL with parameters
     new { controller = "ProductViews", action = "Details", Brand = UrlParameter.Optional, Details = UrlParameter.Optional }// Parameter defaults
    );
    

    You can handle the title, search for the product in db if it’s unique. If you have to add some unique number at the end of the url like;
    http://www.domain.com/gucci/some-gucci-product-name-13456

    routes.MapRoute(
     "Default", // Route name
     "{Brand}/{Details}-{id}", // URL with parameters
     new { controller = "ProductViews", action = "Details", Brand = UrlParameter.Optional, Details = UrlParameter.Optional, id=UrlParameter.Optional }// Parameter defaults
    );
    

    Hope it helps.

    Login or Signup to reply.
  2. @ali is right, but this structure is better-

    /Products/34/my-handbag
    /product/{id}/slug
    

    You can remove the last word in stackoverflow and enter. The page redirects you to current and correct url. It is a better approach. My comments are:

    1. If your website grows up, you can have same slug with different IDs and this is not limit to the problems that may appear. I know that it is not good for seo but it will happen.

    2. In the backend you can have better performance to find product with ID and its important for high visited website.

    3. It will be easy for you to change slug. If you change slug, old link will redirect to new link and you will not have any broken link.

    In the backend find product with ID, then check the database to see if slug is equal to the parameter received from url. If it isn’t, you can redirect with 301 status to correct url, just like stackoverflow.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search