Cakephp 3.1 - How to redirect based on query string values?

落爺英雄遲暮 提交于 2019-12-20 06:15:44

问题


We recently updated our site from pure php to cakephp 3 and having trouble redirecting urls that have params to a new url.

For example this redirect works fine

$routes->redirect('/webcast.php', '/webcast', ['status' => 302]);

But if there's parameters, it doesn't work

$routes->redirect('/webcast.php?id=100', '/webcast', ['status' => 302]);

Any ideas?

Thanks.


回答1:


The router doesn't support matching on query strings, the URL passed to the router when checking for a matching route won't have any query string values attached anymore.

While it would be possible to workaround this by using a custom routing dispatcher filter, a custom/extended Router class, and a custom/extended RouteCollection class, this seems like way too much work for something that can easily be defined as for example a rewrite rule on server level.

Apache mod_rewrite example:

RewriteCond %{QUERY_STRING} ^id=100$
RewriteRule ^webcast\.php$ /webcast? [L,R=302]

And note that 301 is usually the preferred redirect method.

See also

  • Apache > HTTP Server > Documentation > Version 2.4 > Apache mod_rewrite


来源:https://stackoverflow.com/questions/39216483/cakephp-3-1-how-to-redirect-based-on-query-string-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!