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'] = '';
Related
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:
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.
I've been working on a project and been testing it on localhost as well as on a free web server, and it has worked perfectly. However when I got my own domain and web hosting service the site stopped working.
To make it work I had to change the RewriteRule to
RewriteRule ^(.*)$ /index.php?$1 [L]
and then setting
$config['uri_protocol'] = 'QUERY_STRING';
However, links like this one;
http://www.mydomain.com/ajax/membersearch/anemail%40here.com
used to work, but now it returns a CI error saying "The URI you submitted has disallowed characters."
This worked perfectly before I had to change the RewriteRule and uri_protocol, so I'm wondering what is making it not work now, and how to fix it?
You may need to update your config.php file. Replace the default permitted characters setting with the following line:
$config['permitted_uri_chars'] = 'a-z 0-9~%\.\:_\+-,';
I'm trying to track down why my form's post data isn't being passed to the controller.
When I go to the controller's url, the page displays properly. But when I open up firebug it says theres a 404 error.
I'm stumped. How can there be a 404 error when the page is being displayed?
I've checked and double-checked everything. I don't know why this is happening.
UPDATE: This same thing is happening on all my controller's url's. When I visit the site, the page displays just fine, but firebug says 404. I just didn't realize it until now because the other pages aren't accepting any post data.
On the members page, if they're not logged in it redirects. Here, firebug says 302 temporarily moved, which is what it's supposed to do. But when I'm logged in and visit the members page it's 404.
I suspect I need to alter the config->router.php file, but I'm not sure how it needs modified.
In case that link changes, heres the instructions on that page that I followed to get everything working properly:
https://github.com/EllisLab/CodeIgniter/wiki/Godaddy-Installaton-Tips
In config.php:
$config['index_page'] = "index.php?";
$config['uri_protocol'] = "QUERY_STRING";
I've successfully implemented this on GoDaddy without using "index.php?" as the index page. This is helpful when using anchor() in the URL helper to create links. Even with htaccess removing the index.php, anchor() will still put in what you have set as an index page.
You should now be able to access everything as such:
http://yourdomain.com/index.php?controller/action/etc
Once you get to a point where you want to hide the 'index.php?' paste this into your .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
You should now be able to access everything as such:
http://yourdomain.com/controller/action/etc
The .htaccess file must be in Unix LF only style. If you use Notepad or Wordpad and upload an .htaccess file in CR/LF style it won't work.
I am having a problem in implementing jquery.ajax in codeigniter. I want to send the control to a specific function of a controller. I am setting the url in my javascript function like this
var url='<?php echo('First/index');?>';
var ajaxoptions={url:url,success:submit_ajax_response};
First is my controller and index is my function where I want to send the control. When I click on the event on which it is called the following url is formed
http://localhost/codeigniter/First/index
The URL is fine but it is generating the error of 404. I have done such kind of operations various times in zendframework but unable to accomplish this job in codeigniter. I have noticed one thing that if I add index.php in the url it works fine. By adding index.php the url becomes like
http://localhost/codeigniter/index.php/First/index
I am astonished how to remove index.php from route file. I have only two lines in route.php file
$route['default_controller'] = "First";
$route['404_override'] = '';
I have already made my controller as the default controller.
Am I doing correct? What is the problem and how to accomplish this job`
You need to check a couple of things. First, in /application/config/config.php make sure index file is set to this:
$config['index_page'] = '';
Second, make sure you have correct .htaccess. This should be at the root of your public directory (same place as your index.php):
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options FollowSymLinks
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I notice the part that is missing form your URL is the "index.php" thing that CodeIgniter has.
Change your code to this: (You need URL helper, so load that before this):
var url="<?php echo (index_page() . 'First/index');?>";
var ajaxoptions={url:url,success:submit_ajax_response};
index_page returns your site "index" page, as specified in your config file.
In order to remove index.php from your CodeIgniter links see here.