How Do I Add Date (Year/month) Of Blog Post To Url?

So far I’ve got slugs working in my urls, so I can view a single blog post like:


example.com/posts/here-is-an-example-title

Now I want to add the year and month of the blog post to the url. E.g.,


example.com/posts/2012/11/here-is-an-example-title

How do I do this? Additionally how would I have example.com/posts/2012/11 be an index for all posts in that month?

You need to tweak the already existing url manager rules in config/main.php to something like:




'/posts/<year:\d+>/<month:\d+>/<title_slug:.+>' => 'moduleId/controllerId/actionId',



Or whatever suits your requirements… . The above enforces year and month numeric parameters, as well a slug.

Does my action need to accept year and month as parameters? Currently my action only accepts the slug as a parameter because the slug is unique. Or do I have to accept them and just ignore them?

Hi CodeButterfly

for slug also see below

http://www.yiiframework.com/forum/index.php/topic/37385-seo-url-by-title/page__p__180261__fromsearch__1#entry180261

If you want the date or time for SEO reasons then you need to modify your action like actionView($year,$month,$title_slug) {…}

Nope. You do not have to declare such variables but AFAIK they are passed to the action method. Just to complete the picture, you can declare them in the action method with "default values" thus enabling usage of URL with empty vars in relevant locations.