Codeigniter routes not working sometimes

佐手、 提交于 2019-12-25 12:43:16

问题


I have a little question. I am using Codeigniter 2.1.0 and I have the following code in my routes.php.

$route['default_controller'] = "photo";
$route['404_override'] = 'error';

$route['photo/:num'] = 'photo/index/$1';
$route['people/:num'] = 'people/index/$1'; 

The weird thing is, sometimes when I go to http://www.myurl.com/photo/1 it works and othertimes it redirects me to my error page.

Anyone maby has any idea what is wrong with my routes ?

Already thanks in advance!

Bob


回答1:


This is not correct

$route['photo/(:num)'] = 'photo/index/$1';
$route['people/(:num)'] = 'people/index/$1'; 

you have to write this route

$route['photo/(:num)'] = 'photo/$1';
$route['people/(:num)'] = 'people/$1'; 

in that way only number are permitted.

index is not necessary.




回答2:


Not sure if this is the case, but you might want to put parentheses around the wildcards:

$route['default_controller'] = "photo";
$route['404_override'] = 'error';

$route['photo/(:num)'] = 'photo/index/$1';
$route['people/(:num)'] = 'people/index/$1'; 



回答3:


when you use route in Codeigniter, don't forget .httacess file

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

and remove index.php in the config.php file $config['index_page'] = '';



来源:https://stackoverflow.com/questions/8450614/codeigniter-routes-not-working-sometimes

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