3
votes

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.

4
By default if a controller is called without a function it will automatically run the index function. Also why have you got two routes for photos? - Rooneyl
Becuase one is a route for mysite.com/photos which shows a page full of thumbnails and the other is mysite.com/photo/$id which shows a specific photo page when a user clicks on a thumbnail. - Noah Goodrich
I think you may be confusing CI. The second set of rules have $route['photos'] pointing to different controllers, how should it know what one to pick? - Rooneyl
It still doesn't work even with 1 route for $route['photos']. Still get a 403 error. - Noah Goodrich

4 Answers

4
votes

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.

2
votes

OK, let's take a look at these one by one (assuming your domain is 'example.com' and you have 'index.php' hidden via '.htaccess'):

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

This will grab any url like this: http://example.com/photo/36 and route it to the /application/controllers/viewphoto controller, and call the view method and pass it 36 as the parameter.


$route['photo'] = 'photo';

This will grab any url like this: http://example.com/photo and route it to the /application/controllers/photo controller, and call the index method with no parameter.


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

This will grab any url like this: http://example.com/photos and route it to the /application/controllers/photospage controller, and call the index method with no parameter.


$route['photos'] = 'photos';

This will grab any url like this: http://example.com/photos and route it to the /application/controllers/photos controller, and call the index method with no parameter. This route will never get called because it duplicates the one right before it


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

This will grab any url that hasn't been caught thus far and route it to the /application/controllers/homepage controller, and call the index method with no parameter.


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

This will grab any url like this: http://example.com/homepage and route it to the /application/controllers/homepage controller, and call the index method with no parameter.

My guess is this has to do with not having an index method in the controllers. If that is not the case, then we would need to see the contents of the .htaccess file.

0
votes

This problem can occur if you have a directory with a name identical to the route that throws the 403 error. Delete or rename the directory and the route will work.

0
votes

In some cases the issue was caused by triggered ModSecurity rule. You need to whitelisted this rule for your account, so it won't occur again. You can contact with customer support to white list this rule