Hello,
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
Thanks
Keith
(Kburton)
May 3, 2013, 11:28am
2
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',
Keith:
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:
http://www.domain.com/blog/ [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 "[b][color="#9ACD32"]auth0r-name_vinay[/color][color="#FF0000"]~[/color][/b]".
How can I handle this.
Keith
(Kburton)
May 3, 2013, 1:10pm
4
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.
Keith
(Kburton)
May 3, 2013, 1:36pm
6
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',