CodeIgniter always and only shows default controller or 404 page - codeigniter

I am asked to study about CodeIgniter but I haven't even heard about it before, so I'm trying to go through the textbook and official tutorial. But I can't even successfully display a Hellow World...I've tried to search the solution for a while but none of them helps.
Here's the specific problem. I'm building a CodeIgniter environment with MAMP, PHP version is 5.6.2 and CodeIgniter version is 2.2.0. But no matter what I do, the site can only show me the default controller or 404 page.
First of all here's "hello.php" in controller folder:
<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
?>
I haven't set the mod_rewrite yet so I simply goes to this address:
localhost:8888/CodeIgniter/index.php/hello
And I only get a 404 page. So I tried to modify uri_protocol in config.php. I tried all of them, and QUERY_STRING and ORIG_PATH_INFO worked fine at first. I thought I succeeded. But I was wrong. I then setup a mod_rewrite with .htaccess file and I can only get a default controller (which is welcome page). I thought I must did something wrong in my .htaccess file so I tried to edit it but it doesn't work, so I tried to remove the file and go back to index.php/hello again, then I found that it can't work anymore, what I get is only a default welcome page.
I then tried to restart the server, modify index_page in config.php to 'index.php?', move CodeIgniter to root directory (I mean instead of localhost:8888/CodeIgniter/index.php, I removed CodeIgniter folder and just go to localhost:8888/index.php), totally remove the current CodeIgniter folder and download it again from official site, and of course tried other choices of uri_protocol. But nothing helps. What I get is still only default controller or a 404 page.
In summary, in config.php file, if I set
$config['index_page'] = 'index.php';
then
$config['uri_protocol'] = 'QUERY_STRING';
$config['uri_protocol'] = 'ORIG_PATH_INFO';
returns the default controller (even if I try to access a page which actually doesn't exist in controller folder), and other uri_protocols return 404 page.
Else, if I set
$config['index_page'] = 'index.php?';
then
$config['uri_protocol'] = 'PATH_INFO';
$config['uri_protocol'] = 'ORIG_PATH_INFO';
returns the default controller (also even if I try to access a page which doesn't exist in controller folder), and others return 404 page.
Also I thought it may be a problem with MAMP but even if I upload the whole site onto a VPS server (CentOS 6.5, php version 5.3.3) the totally same problem occurs.
Anybody has some idea?

First of all, to remove the index.php from your url, create a .htaccess file in your site root, and add the following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
and then in your config/config.php file :
$config['base_url'] = '';
$config['index_page'] = '';
set the rest config parameters as usual.
Now, you are using the url localhost:8888/CodeIgniter/index.php/hello to access your application. This is wrong (unless you are routing the url). You have to load your controller which in turn loads the view for you. So, now use:
localhost:8888/CodeIgniter/blog
Again. One more thing. In the constructor of your Controller, you will have to call the parent constructor so that you can use loader class to load librariee, view, models whatever.
Now lastly, your best bet is to go through the documentation well. You have tried but not tried enough! Good Luck.

Umm well thanks for your help! A friend on Twitter told me that the class name was wrong, it should be the same as the php file (in this case, it should be class Hello extends CI_Controller)
After that, all things goes OK!

I was having this issue with CodeIgniter 1.7 when using it through the command line (CLI). I had to modify the _fetch_uri_string() function in the CI_URI class (system/libraries/URI.php) to see if $_SERVER['argv'][1] is set to find the URI being used. To find where your project's URI is present with your global $_SERVER variable, I would just do a var_dump($_SERVER); exit(); at the top of your index.php file.
Changes I made to the _fetch_uri_string() function highlighted:

Related

Code igniter - 404 not found, routes issue, ssh server

So first off, I am pretty familiar with codeigniter I've used it for multiple occasions. Right now I have a project too do for school and I installed codeigniter on the server but I am getting routing issues I believe.
So additional info about the server which is different from the normal environment I work with, the server uses SSH and it is very weirdly protected in my opinion. You also need a username and password to view the URL in the first place. (I think it's weird)
If this helps the url is:
https://clipper.encs.concordia.ca/~pyc353_2/
and credentials are (username: pyc353_2, password: FMaqRb)
Now I am able to see any page that I set as my 'default_controller' in the routes, but the problem is trying to go to any other page. Right now I'm trying to access the register controller and I just get 404'd (while it works if I set it as default)
I will link all the code I think may be relevant to the error, hopefully it's not server settings since I have no control over any change.
config.php
$config['base_url'] = 'https://clipper.encs.concordia.ca/~pyc353_2/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
routes.php
$route['default_controller'] = "test";
.htaccess (there may be better code for doing this but I used it in the past no problem and the route doesn't work even if I remove htaccess and add back index.php to 'index_page')
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
</IfModule>
So I tried accessing my pagedifferent variations in the url such as: (stackoverflow won't let me post more than 2 physical links)
clipper.encs.concordia.ca/~pyc353_2/register
clipper.encs.concordia.ca/~pyc353_2/index.php/register
neither work although with /index.php/ the error message is: "No input file specified."
So I hope my explanations are clear enough, it's hard to explain something I don't understand the source problem. If you need any additional info feel free to ask. I really need to solve this to proceed at all for my project.
EDIT
I don't know if this is relevant but the 404 message is not even in the codeigniter style it just looks like a plain web 404
Also I am looking at the path on the server and this is the hierarchy: /www/groups/p/py_comp353_2 I don't know if that could affect anything also?
NEWER EDIT
Following what I found here: http://ellislab.com/codeigniter/user-guide/installation/troubleshooting.html
Changing index.php to index.php? (and removing htaccess) actually did make the routing work. Now I am happy enough with this and I can continue working, but being that it's an ugly solve would anyone know how to keep the routing working AND remove the index.php? from my URI?
It appears that your htaccess is not working.
To confirm, create a new PHP file and write this line:
echo phpinfo();
On that page try to search for "mod_rewrite", if it is enabled, you will see it under "Loaded Modules"
It is probable, it is not enabled.
Try this on your server:
a2enmod enable
Then restart the apache server.
For restarting on debian, this should work:
service apache2 restart
For CentOS, this should work:
apachectl restart
That always does not solve the problem.
Even then, try your CI installation in the normal mode after that. (With everything in normal mode: htaccess as before, and no index.php in the URL)
If it does not work, you may need to alter the apache config file httpd.conf
Try to search for this line:
#LoadModule rewrite_module modules/mod_rewrite.so
Uncomment it by removing the # sign
Restart Apache Server.
Test it now, again.
There could still be other things that you could do to enable mod_rewrite.

CodeIgniter giving a 404 not found on public server only

Description
I have a site "searchnwork.com". It uses CodeIgniter. Every page except the home page loads fine.
If you go to searchnwork.com, it shows the CI 404 error page.
If you go to searchnwork.com/index.php/users, it shows the UserSignUpController page, which is good.
If I set the $route['404_override'] = 'UserSignUpController', I still get a 404.
Everything works fine on my local server.
Question
Why am I getting a 404 for searchnwork.com despite the override?
Why am I getting a 404 for searchnwork.com in the first place instead of my default controller?
Code
$route['users'] = "UserSignUpController"; // This loads fine.
$route['default_controller'] = "UserSignUpController"; // This gives a 404...
$route['404_override'] = 'UserSignUpController'; // This doesn't redirect...
Case sensitive file names.
On MAMP it doesn't seem to care about capitalization. When I uploaded to an actual linux server, CapitalizedFileNames started to fail, since it only searchs for lowercasefilenames. I guess you should use underscores.
Weird that it only fails for index.php.
You need to install a .htaccess on the server root
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Also make sure your /application/config/config.php is set up properly (these can by left blank and should still work properly)
$config['base_url'] = '';
$config['index_page'] = '';

unable to call different class after index in codeigniter

I just started learning codeigniter and came up with some problem I could not sort.
In routing the default controller = 'home'
the base_url()=localhost/CodeIgniter_2.1.3/:
So when the site is loaded the site_url()=localhost/CodeIgniter_2.1.3/index.php
Home controller contains a link , to register controller.
So when this register controller is inside home object, and when the is linked to localhost/CodeIgniter_2.1.3/index.php/register it works fine.
But I want to make my Register controller a different object so that, I can go to register page like this localhost/CodeIgniter_2.1.3/register which I cannot get done. I tried messing with 'routes' but no luck there. Any Ideas?
create in your root directory a .htaccess file with following rules to use URI segments on routing properly :
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
more at codeigniter documentation

In Codeigniter some URIs pass through index.php, some don't

Basically one controller (controller/topics.php, http://192.168.1.50/topics) gets passed through CI's index.php like its suppose to, yet another (controller/user.php, http://192.168.1.50/user) for some odd reason simply doesn't pass (I get an 404 error in browser). CodeIgniter and/or Apache2 are doing something funky and I can't figure it out: '.../user' gives me Apache's 404 page, yet '.../User' gives me CI's 404 page which means CI grabs URIs with uppercase controller names and ignores some URIs with lowercase controller names (and then Apache tries to handle the URI).
Any ideas why and how to resolve?
P.S. - Yes, I did post my issue in CI's forum but I'm not having luck with their help. I'm running CodeIgniter 2.0.2 on a Linux distro (Ubuntu 10.10 with LAMP).
The problem was that the rewrite condition syntax is saying don’t route folders that BEGIN with i, c, j or u instead of folders whose name IS i, c, j or u. Coincidently my controller names which weren’t loading started with i (idea) and u (user).
I could just rename my static folders to their full names (“uploads” instead of “u”) and update the rewrite condition and resolve this issue, but I’d like to keep the names the same. Anyone happen to know the correct syntax for the rewrite condition to match exact names (instead of like or begins with)?
Check your controller name, the most common thing people (I find) do is copy their 'main' controller (say topics.php) and rename the file name to whatever.php
They start developing and the controller name stays 'Topics'. 404 is thrown due to the controller not being found (not the actual file)
ex:
class Blog extends CI_Controller {...
if the above code is in a file called stuff.php. You will get a 404 if you are calling /stuff
I ran into a similar problem yesterday. The default controller can have an uppercase file name but any other controller would produce a 404 when uppercase. This was on a MAMP setup.
Make sure your controller reflects the file name as well.
class User extends CI_Controller {
in your htaccess... instead of
RewriteRule ^(.*)$ /index.php/$1 [L]
try
RewriteRule ^(.*)$ index.php/$1 [L]
and in your CI config set base_url as instructed (include trailing /!)
$config['base_url'] = 'http://www.site.com/';
That should do it.

Codeigniter: Routes only visible when entering /index segments

I'm working on my first project with codeigniter 2.0 and have a bit of a problem.
On my localhost (a MAMP installation) everything works fine with the routes. Only when i add a copy on my domain, change the base url and other necessary settings like my database settings it works fine for like 99%. I can't access my other controllers directly without adding the /index route. For example when i want to visit the http://my_domain.com/work it'll open the 404 error page but when i enter ttp://my_domain.com/work/index it works fine. Does someone know a setting i have to change for the online version? The Htaccess files are identical.
CHeers in advance.
I found my solution. I just checked the routes.php file in my config where i added a route for every controller. I just removed these and only my default_controller route is left. Now it works fine. Just a codeigniter newbie issue.
Thanks anyways for the help #Hibiscus and #BigFatBaby
I sorted my problem by setting the following line as my .htaccess file.
RewriteEngine On
RewriteRule ^.*$ index.php [NC,L]
It'll rewrite anything after the /index.php/blaha to /blaha.

Resources