Allowing Dot(.) To Url


Here I’ve created some rule for URL Manager with regex pattern to allow only alphabets, number, hyphen and under score as:

'blog/<author:[a-zA-Z0-9_-]+>/<title:[a-zA-Z0-9_-]+>/<id:\d+>' => 'blog/read',

'blog/<author:[a-zA-Z0-9_-]+>' => 'blog/listByAuthor',

Above rule works well. But now I want to add one more character to regex and that is dot(.), when I modified rule as below:

'blog/<author:[a-zA-Z0-9_-\.]+>/<title:[a-zA-Z0-9_-\.]+>/<id:\d+>' => 'blog/read',

'blog/<author:[a-zA-Z0-9_-\.]+>' => 'blog/listByAuthor',

Now the problem is by applying above rule php throws an error below:

    preg_match() [<a  href='function.preg-match'>function.preg-match</a>]: 

Compilation failed: range out of order in character class at offset 34

What should I do to allow dot(.) too


Try escaping the hyphen (-) character too. I think it needs to appear at the end of the character class if you don’t want it to be used to define a range.

'blog/<author:[a-zA-Z0-9_-\.]+>/<title:[a-zA-Z0-9_\-\.]+>/<id:\d+>' => 'blog/read',

Thanks a ton Keith.

One more thing.

As above you suggested me solution it works, but when I access URL like this:[color="#9ACD32"]auth0r-name_vinay[/color][color="#FF0000"]~[/color]/some-title-too/007

Then it throws:

[color="#FF0000"]Error 404:[/color]

The system is unable to find the requested action &quot;[b][color=&quot;#9ACD32&quot;]auth0r-name_vinay[/color][color=&quot;#FF0000&quot;]~[/color][/b]&quot;.

How can I handle this.

The tilde (~) character isn’t recognised by your rule, so it’s falling through to a later one.

I know but How can I handle specially for Blog Controller if rule doesn’t match then redirect to some other action specified.

Add another rule underneath to catch blog requests which don’t match your rules?

'blog/<author:[a-zA-Z0-9_-\.]+>/<title:[a-zA-Z0-9_\-\.]+>/<id:\d+>' => 'blog/read',

'blog/<author:[a-zA-Z0-9_-\.]+>' => 'blog/listByAuthor',

'blog/<rest:.*>' => 'destination/action',


You are great.

Thanks a lot again.