Issue with URL Management

I have 2 urls


?r=post/view&type=page&key=about : this is a "static" page

?r=post/view&type=article&key=first-article : this is an article

What I want is to rewrite post of page type and post of article type in diffirent types of URL. For ex,


/about.html (rewrite for ?r=post/view&type=page&key=about)

/article/first-article.html (reqrite for ?r=post/view&type=article&key=first-article)

(I have many other content types, not only page and article).

Is there any way to achieve that? I tried doing the following but it doesn’t work at all:


'components'=>array(

	'urlManager'=>array(

           'urlFormat'=>'path',

	   'showScriptName'=>false,

	   'matchValue'=>true,

           'rules'=>array(

			'<type:[a-z0-9\-]+>/<key:[a-z0-9\-]+>.html' => 'post/view',		// other post view

			'<key:[a-z0-9\-]+>.html' => array('post/view', 'defaultParams'=>array('type'=>'page')),		// static page view

           ),

       ),

Did you configure your .htaccess file?

Also, use ‘urlSuffix’=>’.html’ instead of “.html” in each rule.

instead of using ‘<type:[a-z0-9\-]+>’ try ‘<type:\w+>’ (and the same for <key:…>)

No way! [a-z0-9\-]+ and \w+ are different fules!


           'rules'=>array(

                        '<type:[a-z0-9\-]+>/<key:[a-z0-9\-]+>.html' => 'post/view',             // other post view

                        '<key:[a-z0-9\-]+>.html' => array('post/view', 'defaultParams'=>array('type'=>'page')),         // static page view

           ),

You should try it, then you will know why it doesn’t work. For ex, array(‘post/view’, ‘type’=>‘page’, ‘key’=>‘xyz’) will produce url /page/xyz.html . That’s not what I want. The problem here is that we cannot use the same route for 2 diferrent rules.

…// sorry, please delete this duplicate post

Before digging into the regex, let me ask one question: Why don’t you use a separate action to serve your static pages? They are two different things that should also be separated in your controller. That would safe you a lot of hassle.

Can you try this (change: the first rule):




'components'=>array(

	'urlManager'=>array(

		'urlFormat'=>'path',

		'showScriptName'=>false,

		'matchValue'=>true,

		'rules'=>array(

			'<type:article>/<key:[a-z0-9\-]+>.html' => 'post/view',		// other post view

			'<key:[a-z0-9\-]+>.html' => array('post/view', 'defaultParams'=>array('type'=>'page')),

												// static page view

           ),

       ),