CodeIgniter doesn't load default controller on '/' - php

I'm using CodeIgniter with Nginx.
I have just upgraded from CodeIgniter 2.2.6 to 3.1.4, following all the changelogs and upgrade instruction. Everything works perfectly but the default controller, i.e. http://francescoruvolo.it (or even http://francescoruvolo.it/index.php) shows a 404 page but all other routing rules works and I'm able to load the other controllers.
Here is my routes.php:
$route['(it|en)/contact/check_form'] = "Email/validate_form/$1";
$route['(it|en)/contact'] = "Email/show_form/$1";
$route['(it|en)/(:any)'] = "Pages/show/$1/$2";
$route['(it|en)'] = "Pages/show/$1";
$route['default_controller'] = "Pages/show/";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I have also tried to comment all routes, but still the default one is ignored. Still, if I go http://francescoruvolo.it/Pages/show or either http://francescoruvolo.it/en, controllers are loaded just fine and it works. Many people had similar problems because they missed the part about enforcing the capitalized name for classes, but this is not the case as my controllers' name have been already fixed.
These are the parameters in config.php:
$config['base_url'] = 'http://francescoruvolo.it/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
And Nginx looks like it is properly forwarding all requests to index.php, in fact I don't need to specify it when loading controllers explicitly. Anyway, these lines are part of my Nginx configuration for the host:
location / {
try_files $uri $uri/ /index.php;
}
What I don't get is that I'm actually able to get two different kind of 404 pages. For example, if I go to http://francescoruvolo.it/nonexistent.php Nginx correctly serves me a 404 page. At the same time, if I do not specify any path or explicitly specify index.php I get a 404 "well formatted", which means that the index.php from CodeIgniter has been actually run, but it failed to load the controller.
What am I missing? What else can I check?

Solved. Apparently, I had an extra (unneeded) / at the end of my default route.
This was WRONG:
$route['default_controller'] = "Pages/show/";
This is CORRECT:
$route['default_controller'] = "Pages/show";
Still, I don't quite get why. In fact, if I explicitly load the controller, it works even with the extra slash.
If anyone explains this, I'll gladly accept his answer. :)

Related

Page not found after update to Codeigniter 3

I am trying to get rid of the 404 Page not found error while updating to Codeigniter 3.0
I am using Ion Auth and HMVC libraries. I have fixed Ion Auth issue and I believe (not so sure) that I have fixed HMVC issues as well. If not than don't know how to debug ^_^ it. But I have updated both libraries with the latest one from their respective repos.
I have logged in successfully but when system try to loads the first page after login it is giving 404 error and here are the logs
DEBUG - 2015-03-17 10:51:51 --> UTF-8 Support Enabled
DEBUG - 2015-03-17 10:51:51 --> Global POST, GET and COOKIE data sanitized
ERROR - 2015-03-17 10:51:51 --> 404 Page Not Found: ../modules/admin/controllers/dashboard//index
Notice: double slash in URI before last segment (index). Isn't it strange?
I wonder what is wrong here. I have tried uri_protocol by setting AUTO and REQUEST_URI too but none of them worked.
$config['uri_protocol'] = 'AUTO';
and
$config['uri_protocol'] = 'REQUEST_URI';
Now, I my development completely stuck. Can anyone help me to rid of this issue? Thanks a lot...!
Routes info
application/config/routes.php
$route['default_controller'] = "users/login";
$route['404_override'] = '';
application/modules/users/config/routes.php This is for Ion Auth
$route['users/(:any)'] = "auth/$1";
I have fixed my 404 redirect issue by capitalizing all my class file names in the controller folder and model folder.
So instead of main.php I did Main.php.
Hope this helps someone moving from CI 2 to CI 3.0
Capitalize all your controllers and models files.
Ex:
- controllers/Ajax.php
class Ajax extends MX_Controller {
... some magic stuff
}

Codeigniter 404_override not working in routes

I try almost everything for fixed my problem but I can't
This is my routes file in codeigniter.
$route['default_controller'] = "main";
$route['404_override'] = 'my_error_page';
$route['(:any)'] = 'page/index/$1/';
My website working well normally but 404_override not redirect to my_error_page.
I see standart codeigniter 404 page like the picture
When I delete last line like following;
$route['default_controller'] = "main";
$route['404_override'] = 'my_error_page';
404_override redirect to my error page and of course web site not running well.
It is maybe a simple problem but I am confused.
Sorry bad english.
Please help.
Thaank you.

Codeigniter with single controller error 404

I'm quite a rookie with CodeIgniter, and as per title, I have troubles trying to setup a single controller for my application. It's a very simple static site with couple of pages like "home", "about" and so on...
I have this in my routes.php file:
$route['default_controller'] = "mycontroller";
$route['404_override'] = '';
$route['(:any)'] = "mycontroller/$1";
And this in mycontroller.php file:
// Home
public function index()
{
$data['page'] = 'home';
$this->load->view('template',$data);
}
// about
public function about()
{
$data['page'] = 'about';
$data['title'] = 'About Us';
$this->load->view('template',$data);
}
I'm working in a localhost environment, and the CI project is in this folder:
http://localhost/local/project/ci-tbs/
and I've specified it also in the config.php file for the base_url parameter.
Now what I'd expect pointing the browser to
http://localhost/local/project/ci-tbs/about
is to find the "About Us" page, instead I got a 404 error. Pointing to the base address corectly gives me the "Home" page.
What am I doing wrong?
Is it sensed to use a single controller istead of 1 per page? I'd totally do that in a quick way to fix, still I'm quite baffled by the fact that I can't understand what I am doing wrong and why it's not working. I'd like to simple set everything in one controller, one method per page.
I've already seen this topic asked here in SO, like using regular expressions in the route $route['(.*)'] = "mycontroller/$1";, but nothing really worked for my case wich I think is quite basic (so basic I'm sure my error is so gross that it will be quite embarrassing :P ).
Additional info:
I have in the folder an .htaccess file picked as is from the Html5 Boilerplate, tried with and without it but 404 is always there. I'm using XAMPP as local environment.
For answer
As mentioned by #Vincent Decaux in the answer, the deal to fix this was to add index.php in the url, the other interesting part is
Create your .htaccess file to "hide" index.php
This way I've resolved another small issue for the pages with missing findings for the assets files, so I used the following rule in the .htaccess file, redirecting all requests to the index.php file and excluding files in assets folder and images, along with robots.txt as suggested here https://stackoverflow.com/a/11846150/1262357
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
hope this helps others with same problems I had!
As mentionned in my comment, it seems to work using :
localhost/local/project/ci-tbs/index.php/about
Create your .htaccess file to "hide" index.php.

Codeigniter, bypassing the main or default controller

i tried to find solution, but i think i am quite doing something wrong here,
i hope any one with good knowledge of codeigniter routing can help me in this regard.
what i want is that,
i have default controller named main
$route['default_controller'] = "main";
$route['404_override'] = '';
my site urls are like
mydomain.com/main/#home
mydomain.com/main/#search
mydomain.com/main/#login
what i want is to remove/hide the main from center and links can directly work.
like this
mydomain.com/#home
however i did succeeded a little using codeigniter documentation.
This is what i did to achieve it.
in route file i added this
$route['(:any)'] = "main/$1";
it worked but it messed up with other links.
First now domain.com/main/#home stopped working.
second, now i cant have other controller name here??
i mean if i try this domain.com/virtualvault/#search, it wont work..
What i am trying to have that,
When i go to domain.com/main/#home it should go to home page
and also when i try domain.com/#home it should also go to same home page
not to forget that i want other controllers to be working fully.
i mean, domain.com/othercontrollername/#function should work.
what and how to achieve it, .htaccess or route php file..
i even tried this below route code.
$route['#+(:any)'] = "main/#+$1";
but i am no good in this routing or .htaccess files.
any ideas how to achieve it??
// The below line is for the old controller to work as before
$route['main/(:any)'] = "main/$1";
//This line is for making other controllers work as before, you have to put them all
$route['virtualvault/(:any)'] = "virtualvault/$1";
//This is the last line; the order is important
$route['(:any)'] = "main/$1";
If you need only those three URLs (home, search and login) to work without the controller name, just put the three of them in routes.php ($route['login'] = "main/login";, etc.) and use no wildcards in order to prevent other URLs from getting rerouted.
So, all of them should be:
$route['home'] = "main/home";
$route['search'] = "main/search";
$route['login'] = "main/login";

default_controller (in routes.php) not working in production

Regarding codeigniter routes.php :
We have following entry in C:\wamp\www\application\config\routes.php
$route['default_controller'] = 'latestC';
$route['404_override'] = 'latestC';
and latestC is our default controller. Here default_controller is not working in production. If we remove line $route['404_override'] = 'latestC'; from routes.php, we are not able to reach to home page while hitting main url mozvo.com and its a 404. Basically 404_override is doing job for us instead of default_controller for taking to homepage on hitting mozvo.com. Requests are routing to home page by 404_override controller.
But in localhost, it works perfectly. In localhost if we remove 404_controller, default_controller takes care of main url (mozvo.com, here localhost ) and others non supported urls are 404 which is correct. But in production default_controller is not taking to homepage(mozvo.com) properly so we are forced to use 404_override to take default request to homepage.
Additional Info - Entries in C:\wamp\www\application\config\config.php
$config['base_url'] = 'http://mozvo.com/';
$config['index_page'] = '';
I bet you the issue is due to case-sensitivity on files. Your local host is on WAMP - which windows does does not care about file cases.
i.e. latestC.php = latestc.php = LASTESTC.php
but on your production server (which I'm guessing is a LAMP) - case sensitivity DOES matter
i.e. latestC.php != latestc.php != LASTESTC.php
All your controllers must be LOWERCASE for Codeigniter. So change your routes to
$route['default_controller'] = 'latestc'; // all lowercase
$route['404_override'] = 'latestc'; //all lowercase
and make sure all your files are all lowercase

Categories