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