3

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
    Jan 5, 2012 at 11:22
  • 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. Jan 5, 2012 at 11:29
  • 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
    Jan 5, 2012 at 11:37
  • It still doesn't work even with 1 route for $route['photos']. Still get a 403 error. Jan 5, 2012 at 11:46

3 Answers 3

5

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.

3
  • This almost works. I had to remove the \ before the index.php on the 3rd line though, as my site is stored in a subdirectory from my main domain for dev purposes and it just starts loading the directory above... The routing now works but no images or CSS are loaded... Jan 5, 2012 at 16:40
  • 1
    Ok I have figured it out! There were two issues: 1) I forgot to add the css directory into the 2nd line of the rewrite. 2) I have a folder called 'photos' which stores a load of photos that are used on the pages (which i've now renamed), I guess it was conflicting with the routing. Thanks Jakub! Jan 5, 2012 at 17:16
  • 1
    In my case it was the directory with the name identical to the route that caused this issue. Deleting the (unneeded) directory solved the problem. Sep 22, 2014 at 6:51
2

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.

3
  • I don't think it has anything to do with the index function. I changed it to a view function and it still doesn't work. It should work though, I don't get why it doesnt. Here's the contents of my .htaccess file (which I have modified to hide the index.php): RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)$ index.php?/$1 [L] Jan 5, 2012 at 16:17
  • I have updated my question with an updated routes config file. Still get the same 403 error! Jan 5, 2012 at 16:20
  • Then maybe it's your photos controller. What does that look like?
    – swatkins
    Jan 5, 2012 at 16:35
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.