Codeigniter regexp routing

南楼画角 提交于 2020-01-14 02:02:09

问题


I have a problem with routing like this:

$route['(/[a-z]{2}/)'] = 'locale/somepage';

And in .htaccess

RewriteEngine on
RewriteBase /
RewriteRule ^(/[a-z]{2}/)$ /index.php/locale/somepage

I need replace first section of url (class or controller) and call an another controller. For example, I need to url as /en/page will call controller locale, but url need not be changed.

This code is not working. And if I try use only routes.php or only .htaccess, it not working too. How I can make it work?


回答1:


I think you've written your htaccess regexp rule wrong. You don't need to write it as a PHP regexp rule, try to rewrite it without the slash in your htaccess:

RewriteRule ^([a-z]{2})/page$ /index.php/locale/somepage

This will send any http://mypage.com/en/page to index.php/locale/somepage.

With that rule, CI will receive the url index.php/locale/somepage. At that point, CI will go to routes.php and will check if there are any rule to call a specific controller. If not, it'll try to go to a controller called locale, to load a method called somepage.

So, you don't need to use routes.php to modify again the apache url that you're receving to call another controller.



来源:https://stackoverflow.com/questions/22323341/codeigniter-regexp-routing

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