UrlManager optional parameter

I’m trying to set an optional parameter to UrlManager route.

'<lang:[a-z]{2}>/artykuly/<a:\d+>-<link:([a-zA-Z0-9\-]+)>'=>array('article/index/view', 'urlSuffix'=>'.html'),

My goal is to set a route to work with and without lang parameter in address. If I set that param application will work in language from lang param, if not - in default language (sourceLanguage).

What should I add to this route?

You could try something like


then you would have to remove the forward slash from the ‘lang’ variable if it is not empty.

I’m not sure how CUrlRule will handle the empty string, but it’s worth a try.

Did you mean:



I was trying to do it like that, but nothing works:( Any suggestions…?

I meant for you to try exactly what I put in the original post. I added the pipe symbol "|" to denote the "OR" operator. It should translate the lang variable to: any two letters followed by a slash OR an empty string (nothing). It thought it would be worth a shot. If it works you should get a lang variable passed either as two letters and a slash or an empty string.

I was trying it too. Doesn’t work.


I was trying that way too. It looks CUrlManager won’t work with empty strings in rule (it returns Exception that rule is not correct).

Simply add another rule without the lang parameter.

Yes, I know that.

I was looking for solution without writing another rules.

But, if Yii DevTeam member says there is no other solution, I think I should write another rules for that :)

Thanks for reply.

Is there a way to do it without writing another rule that excludes the optional param?

I tried using: <x:\d?> and <x:\d*>

Neither of those worked

A bit too late but you can you the pattern parameter to specify a regex that should be used during match url.

see this (and the source for CUrlRule for details)