codeigniter正则表达式的路由 [英] Codeigniter regexp routing
问题描述
我有一个问题,像这样的路由:
I have a problem with routing like this:
$route['(/[a-z]{2}/)'] = 'locale/somepage';
和在.htaccess
And in .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^(/[a-z]{2}/)$ /index.php/locale/somepage
我需要更换URL(类或控制器)的第一部分,并调用另一个控制器。例如,我需要网址 / EN /页将呼叫控制器的区域设置,但URL不需要更改。
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.
这code不工作。如果我尝试只用routes.php文件或仅htaccess的,它不是工作压力太大。 我怎样才能使它工作?
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?
推荐答案
我想你写你的htaccess正则表达式规则错误。你不需要写它作为一个PHP的正则表达式规则,尝试重写它不会在你的htaccess斜线:
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
这将发送任何 http://mypage.com/en/page
来的index.php /区域/ SomePage的。
This will send any http://mypage.com/en/page
to index.php/locale/somepage.
通过这条规则,CI将接收URL 的index.php /区域/ SomePage的
。在这一点上,CI会去 routes.php文件
,并检查是否有任何规则调用特定的控制器。如果不是,它会尝试去到一个名为区域
控制器,加载一个名为方法 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
.
所以,你不需要使用 routes.php文件
再修改Apache的URL,你receving调用另一个控制器。
So, you don't need to use routes.php
to modify again the apache url that you're receving to call another controller.
这篇关于codeigniter正则表达式的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!