问题
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