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.
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.
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.
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.
Related
Please can any one suggest how to shorten the url
http://localhost:8080/MyWebApp/index.php/Cpanel_control/
to
http://localhost:8080/MyWebApp/Cpanel
in Codeigniter using routes.
I tried it in this way
$route['Cpanel'] = "MyWebApp/index/Cpanel_control";
But did not work
To remove index.php from your url in CI, you need .htaccess file.
Check this out https://gist.github.com/philipptempel/4226750
I'm assuming Cpanel_control is a valid controller.
For the routing, you can have this in your routes settings
$route['Cpanel'] = "Cpanel_control";
To avoid any other issues, make sure base_url in config file is set thus
$config['base_url'] = "http://localhost:8080/MyWebApp";
New Approach
Routing:
In some instances, however, you may want to remap this relationship so
that a different class/method can be called instead of the one
corresponding to the URL.
A short excerpt from the CI doc shows that you can't use routing here. Instead you should go for a mod_rewrite rule (.htaccess)
RewriteEngine On
RewriteRule ^index/Cpanel_control/(.*) Cpanel/$1 [R]
Old approach
According to the corresponding documentation, the paths are both not absolute. Furthermore, you need to set the "from"-URI as the array key and the "to" URI as the string. If you want to route index/Cpanel_control to Cpanel, you need to swap the URIs of you example.
So this would be correct:
$route['index/Cpanel_control'] = "Cpanel";
I am new to CodeIgniter and I have some issues with its routing config.
Everything works fine if I access a page as it should be
http://localhost/index.php/class/controller
But if I add some random arguments
http://localhost/index.php/class/controller/randomStuff
it displays the page without the CSS or the JS but I would like to display a 404 error page.
Here is my route.php file
$route['stats'] = 'main/account';
$route['/'] = 'index.php';
$route['/(:any)'] = 'main/disperr'; //Tried this to solve the problem but doesn't work (disperr simply returns show_404())
$route['default_controller'] = 'main';
$route['all'] = 'main';
$route['404_override'] = 'errors/page_missing';
$route['translate_uri_dashes'] = FALSE;
Thanks in advance for your help.
Edit : Thank you for your answers !
After routing the pages correctly I stil had the missing css issue.
As I'm using twig, I sent the base_url() as a parameter to my view and added it to the path to my CSS and JS.
It works now.
you may plese the following url
http://localhost/class/index.php/main
after index.php you need to give your name of the controller(not folder name controller)
You have to follow some rules from documentation page.
Routes you found after CodeIgniter installation have to be at the beginning in order you found those.
$route['default_controller'] = 'main';//Main class need to have index() method for this works
$route['404_override'] = 'errors/page_missing';
$route['translate_uri_dashes'] = FALSE;
You have to take care of precedence because rules will be read in order those are defined in file.
$route['all'] = 'main';
$route['stats'] = 'main/account';
$route['(:any)'] = 'main/disperr'; //Tried this to solve the problem but doesn't work (disperr simply returns show_404())
If you have (:any) wildcard placeholder it has to be at the end of file because you have to check all specific routes not to pass (:any) rule, because that rule is valid for any route (as name suggest). Slash to index route is not needed because slash is default controller and it is already defined. Routing to index.php is not valid and you are suppose to route just to 'controller_name/method_name/param1' or similar pattern. No need for slashes at the beginning, as you see I removed those. Maybe removing slashes from the beginning of made routes will solve your CSS issue, but you need to follow other documentation rules I exposed here. Read full page here.
I am using codeigniter to create a restapi and I am having some problems with routes.
Here is the equation:
I have to navigate /users/ to index function,
I have to navigate /users/{MongoId} to /users/show/{MongoId},
I have to navigate /users/function to /users/function.
And here, my routes:
$route['api/users/do_login'] = "api/users/do_login";
$route['api/users/(.*)'] = "api/users/show/$1";
When I remove the (.*) routing (or both of them), my do_login function works successfully. But not my api-index function because Codeigniter takes MongoId as function name and fails.
When I write it back (or both), my index function works successfully, but my login doesn't.
Because it tries to send function name to show function as parameter.
Can you please help me to fix that?
Reverse the order of routes, CodeIgniters routes are prioritized.
You seek this structure:
$route['api/users/(:any)'] = "api/users/show/$1";
$route['api/users/do_login'] = "api/users/do_login";
also use (:any) instead of (.*) they are the same.
CodeIgniter Routing
Here, the working routes.
$route['api/users/do_login/(:any)'] = "api/users/do_login/$1";
$route['api/users/(:any)'] = "api/users/show/$1";
I am having trouble removing the controller name from my url path on my localhost.
i have this url - localhost:8888/localhost/site_name/
i have been able to remove index.php from the url using my htaccess similar to http://codeigniter.com/wiki/mod_rewrite so that:
localhost:8888/localhost/site_name/index.php/controller_name
is now:
localhost:8888/localhost/site_name/controller_name/
but i can't remove the controller name from the path so that:
localhost:8888/localhost/site_name/controller_name/function_name/
becomes:
localhost:8888/localhost/site_name/function_name/
I am using only one controller, and i have added:
$route['^(function_name1|function_name2|function_name3)(/:any)?$'] = 'controller_name/$0';
$route['^(?!ezstore|ezsell|login).*'] = "home/$0"; /*similar variation i tried*/
and other variations to my routes file but it does not have any effect. i also tried using the _remap function but that does not help in this case.
Any help will be appreciated! Thanks
You can use a wildcard route,
$route['(:any)'] = "controller_name/$1";
Then when if you go to http://localhost/function_one/param1
it will call the controller controller_name the function function_once and pass param1 as the first parameter.
nb: I must point out, using only one controller for an entire site does raise warning bells for me, you may want to get your code design checked out, but that's just me.
Ok, so maybe that title is a bit confusing. What I'd like to do is trap any info, if any after the first URI segement if that segment is something specific and forward that on to another controller. Here's my routes file:
$route['default_controller'] = "main";
$route['404_override'] = '';
$route['testroute'] = "main";
So, what this does right now is, if I got to mydomain.com/testroute it shows me the default page, which it should. However, what I'd like is if I go to mydomain.com/testroute/testmethod/ where testmethod is a method in the main controller I'd like it to forward that as well. So basically I'd like it to route to the main controller regardless of if there are more segments after the testroute, but if there are they should get passed as method calls of the main controller.
Simply catch the parameter given and pass it to the controller e.g. like this:
$route['testroute/(:any)'] = "main/$1";
(:any) actually catches any type of string. There are other selectors, as well. More on this topic can be found here.
Edit (answer to your comment):
If you want a general route to the index() method of your main controller, just add both routes:
$route['testroute'] = "main";
$route['testroute/(:any)'] = "main/$1";
Uh, what? That's how CodeIgniter's controllers work already. Show us some code? What isn't working about it?
Have you configured URL rewriting in your .htaccess file so you don't need the /index.php/testroute in the URL?
Open .htaccess, and try this code if you haven't already rewritten this:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
If you haven't configured rewriting then it won't work without that^, but you can access like mydomain.com/path/to/ci/index.php/testroute/testmethod