doushifang4382 2014-02-04 17:25
浏览 117
已采纳

Yii:删除不必要的URL参数

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 :-)

  • 写回答

1条回答 默认 最新

  • duanche2007 2014-02-04 17:35
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思