Tuts20's avatar

SEO friendly URL - routes

Hello guys,

what is the best way to implement SEO friendly urls? For example I have an article detail which looks like this: http://localhost:8888/project/public/detail/1 or http://localhost:8888/project/public/detail/2

So my route looks like this:

Route::get('detail/{id}', 'SiteController@article');

Now I know this does not look really good, so naturally I would like to have the urls more like:

http://localhost:8888/project/public/detail/title-of-the-article

But not sure about what is correct way of doing that? I am thinking I somehow need to use a slug which will somehow automatically replaced those "ids" with " articles titles"?

Thank you

0 likes
4 replies
Snapey's avatar

Fix your web server. You should not see project/public in the URL. You need to set the documentRoot to your public folder.

If you use Route Model Binding then you can switch from using id to using slug by setting getRouteKeyName on your model. See

https://laravel.com/docs/5.4/routing#route-model-binding

So basics;

  • create a column on your table for the slug
  • create a slug by using the slug helper and save in table
  • set getRouteKeyName on your model
  • for clarity, change the route to detail/{slug}
  • when creating a link to the page, set the route using the slug column nstead of id
2 likes
shubhashishitservices's avatar

If your website dynamic your change your URL by using admin panel and if it's static you should be rewrite your URL path

Please or to participate in this conversation.