I have a problem: I have programed my application with SEO friendly URLs, according to the manual in the following way:
http://example.com/page/page-slug
with the following URLManager rules:
'page/<slug:.*?>/<page:.*?>'=>'page/view',
'page/<slug:.*?>/'=>'page/view',
Everything works fine, but when the link is shared (i don't know actually where), some additionar parameters are added in a freakish way:
http://example.com/page/page-slug&locale=en_us&mobile=true&numposts=5
and when that happens, the page throws an error
Invalid argument supplied for foreach()
traced to a private function inside my controller that doesn't has anything to do with this.
So my question goes to:
How can I sanitize the request url in the beforeAction? Is there any way?
Thanks in advance :-)
The first thing I would do would be improve the rules so that they only accept expected values. Currently you're letting almost anything though. Something along the lines of:
'page/<slug:\w+>/<page:[a-z\-]+>'=>'page/view',
'page/<slug:\w+>/'=>'page/view',
By doing so you ensure that...
http://example.com/page/page-slug&locale=en_us&mobile=true&numposts=5
...results in a 404 as the URL doesn't match your rules.
However that URL is malformed anyway, as the first ampersand should be a question mark. So if you are expecting bad URLs, you can look out for them. This is a very rough bit of regex, but you get the idea:
'page/<slug:\w+>/<page:[a-z\-]+>&<rubbish:.*>'=>'page/viewWithBadUrls',
And in your actionViewWithBadUrls() controller method you'd get:
print_r($_GET); // Array ( [slug] => page [page] => page-slug [rubbish] => locale=en_us&mobile=true&numposts=5 )
Which you could use, or clean up the URL and 301 to the correct place