How to set category and product title base SEO friendly url in Yii2

Dear All,

I’m using yii2 latest version and i have developed catalog(category-product base) base application and want to make SEO friendly urls in that. i tried to make url but didn’t get success on it.

So, can you please help me on this?

Thanks

Dharmesh

Could you provide a couple of different catalog, category, product examples to help with a better idea of what you are trying to do.

Take a look at the Sluggable behavior it may help the product part.

Thanks for reply,

Below is some examples, which i want to make SEO url from Real url.

Category Url:

==========================

Category Name: Food And Baverages

Real Url: http://xyz.com/index.php?r=category/list/&id=1

Seo Url: ttp://xyz.com/food-and-baverages.html

Sub Category Url:

==========================

Category Name: Food And Baverages

Sub Category Name: Baby Food

Real Url: http://xyz.com/index.php?r=subcategory/list&id=1

Seo Url: ttp://xyz.com/food-and-baverages/baby-food.html

Product Url:

==========================

Category Name: Food And Baverages

Sub Category Name: Baby Food

Product Name: Pizza

Real Url: http://xyz.com/index.php?r=product/detail&id=1

Seo Url: ttp://xyz.com/food-and-baverages/baby-food/pizza.html

Thanks

[size="2"]To remove the [/size]initial[size="2"] index.php from all of your [/size]URL[size="2"]s read this [/size]http://www.bsourceco…iframework-2-0/

You should read this routing and URL creating docs http://www.yiiframew…me-routing.html it shows you all of this and discusses performance and other issues.

One thing to consider is that you do not allow for any controllers to match the name of your categories, sub categories or products or you will end up with links that don’t work. You will need to figure out how you want to handle this. Maybe do a validation rule that doesn’t allow for certain words. You could also prefix all of your categories, sub categories, and products with the id to avoid having to do that but it won’t match your desired output.

In the URL you will need to have a way to get your category,subcategory, and product names to the URLs.

You will need to have a field in each of your tables that will hold the url slug. A easy way to create these is to use the slugable behavior http://www.yiiframew…lebehavior.html. You will need to be careful as you are trying to create SEO friendly pages and when a slug changes your page URL will change. If it was indexed by a search engine you could loose some rank. A way to avoid this is to add the id like i stated above. This will match the id and not the slug so if the slug changes the url will always work because the id never changes. for example look at the stack overflow url https://stackoverflo…-url-a-bad-idea it has the id in the URL as does Google, Facebook etc. so it’s just a matter of research and preference for your application. I personally use ID in the URLs.

Url Manager





'urlManager' => [

    'enablePrettyUrl' => true,

    'showScriptName' => false,

    //all of your urls have .html so this is here but you can remove it if you don't want it

    'suffix' => '.html',

    'rules' => [

        //need to be in order so you don't match the wrong url

        //your url is not proper for category list it is ...list/&id=1 and should be list&id=1 

        '<category:\w+>' => 'category/list',

        '<category:\w+>/<subcategory:\w+>' => 'subcategory/list',

        '<category:\w+>/<subcategory:\w+>/<name:\w+>' => 'product/detail'

    ]

]



Controller actions




//category list

public function actionList($id,$category){

}

//subcategory list

public function actionList($id,$category,$subcategory){

}

//product detail

public function actionDetail($id,$category,$subcategory,$name){

}



Create a URL




//category list

Url::to(['/category/list','id'=>$id,'category'=>$category]);

//subcategory list

Url::to(['/subcategory/list','id'=>$id,'category'=>$category, 'subcategory'=>$subcategory]);

//product detail

Url::to(['/product/detail','id'=>$id,'category'=>$category, 'subcategory'=>$subcategory,'name'=>$name]);



Thanks for reply,

Will try it and inform you.