Urlmanager rule to create static url

I have a controlled called “Posts” I use this controller in backend to create pages like, ‘about’ (id = 1) and ‘contact’ (id=2)

In frontend to view this pages I use a url like this "domain.com/posts/view/1", I want to create a rule to get something like this "domain.com/about"

How can I create a rule to do this?

I already tried this:

 'about' => 'posts/view/1',

'contact' => 'posts/view/2',

But doesn’t work.

It seems, that you’re looking for something like URL aliases. For example, like those which allows you to access particular page using URL like wordpress.mydomain.com/title-of-page instead of wordpress.mydomain.com/page.php?id=134 (URL examples totally out of my imagination).

I have a ready solution for this problem, but since I’m just starting my play in Yii2, it is written for Yii1. If you want to take a look and port it to Yii2, let me know, and I paste some code examples here.

Create a slug field in your db.

Tell it to load by slug not the id in your controller.

some people like yoursite.com/1/some-title that way you don’t get hit on search engines for “broken” pages when someone changes the tile/slug attribute.



then someone changes your about page to about us


now search engines have your about page indexed as about instead of about-us. Thus throwing a 404 page and lowering your rankings.

You can make it so the slug cant ever be changed so you don’t have to be worried about rankings.

Use the method that leave an id / slug.

Or just let it be and not worry about rankings.

//slug is not required to load page. this method is used for site.com/1/some-slug

	public function actionView($id, $slug = NULL) {

    	$model = $this->findModel($id);


	protected function findModel($id) {

    	if (($model =Post::findOne($id)) !== null) {

        	return $model;

    	} else {

        	throw new NotFoundHttpException('The requested page does not exist.');




public function actionView($slug) {

    	$model = $this->findModel($slug);


 protected function findModel($slug) {

    	if (($model =Post::findOne($slug)) !== null) {

        	return $model;

    	} else {

        	throw new NotFoundHttpException('The requested page does not exist.');



make sure you are calling your urls correctly too .i.e

<?= Html::a($model->title,['post/view','id'=>$model->id, 'slug'=>$model->slug]);


<?= Html::a($model->title,['post/view', 'slug'=>$model->slug]);

call it normally and let the urlmanager correct them. that way if you ever change your rule you don’t have to change all of your buttons, links etc.

Use a behavior or function in your model to save the slugs into your db.


Yii2’s built in sluggable behavior

Use a rewrite rule for the slug not Id. Also, you want the slugs to be called dynamically not statically or you will have to create a rule for every single page.

 'post/<id:\d+>/<slug>' => 'post/view',


 'post/<slug>' => 'post/view',

Thank you for your replies. I found a solution.

I add this to my rules

'<id:about-us>' => 'posts/view',

Now everything is working fine ;)