Search code examples
codeigniter

403 forbidden with URI routing in Codeigniter


I'm trying to set up routing to a page on my site with Codeigniter, but I just get a 403. I can't understand why.

Code in the routes config file is:

$route['photo/(:num)'] = "viewphoto/view/$1";
$route['photo'] = 'photo';

$route['photos'] = "photospage/index";
$route['photos'] = 'photos';

$route['default_controller'] = 'homepage';
$route['homepage'] = 'homepage';

When going to mysite.com/photo/2 (for example) it works fine, as does the homepage. But when going to mysite.com/photos I just get a 403 Forbidden error message.

I can't work it out, the routing is set to exctly the same as the mysite.com/photo/2 routing.

The controller it's pointing to is called photospage and the function inside it is called index.

If I go to mysite.com/photos/index it works though...

Any help is most appreciated :)

EDIT:

Change the routes config file to the following but it still doesn't work when I go to mysite.com/photos. I changed the controller function to a 'view' instead of 'index' but it still won't work :(

$route['photos'] = "photos/view";
$route['photo/(:num)'] = "viewphoto/view/$1";
$route['default_controller'] = 'homepage';

homepage and photo/$id still work fine though.


Solution

  • Your rewrite rule should be:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    You have to remove your index.php entry from your /application/config/config.php

    Verify that is setup correctly the 403 forbidden error might be due to a faulty .htaccess rewrite.