Codeigniter can only access default route controller - php

I suspect this may be a webserver configuration change that has taken place, and my hosting provider hasn't told anyone, but anyway...
I have a Codeigniter instance set up with the following directory structure:
/root
/codeigniter
/app
/sys
/www
/sandbox
/ci
/js
/css
/img
.htaccess
index.php
Therefore in index.php, I have the following variables:
$application_folder = '../../../codeigniter/app';
$system_path = '../../../codeigniter/sys';
These are relative definitions due to it being a shared hosting provider, however it does appear to be working that way so I don't think this is the issue (in WAMP i have those folder paths explicitly defined, e.g. c:/wamp/ci/application)
In config.php, I have the following variables set:
$config['base_url'] = 'http://******.co.uk/sandbox/ci/';
...
$config['index_page'] = '';
...
$config['uri_protocol'] = 'AUTO';
and my .htaccess file in www/sandbox/ci is set like so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Up until a few days ago, this all worked fine, however, I replaced a fair few files just before Christmas, and may have accidentally overwritten something without realising it. This setup works fine on my WAMP server, but fails on my live server If I attempt to access any function in any controller that isn't the one defined in routes.php.
My routes.php looks like this:
$route['default_controller'] = "home_controller";
$route['404_override'] = '';
home_controller appears fine, however if I attempt to do something like login (which is processed via a form POST to auth_controller/login), it fails with 404. I do not have any additional functions in home_controller.
I get nothing interesting from setting the logging threshold to 4, and ChromePHP just suggests that it cannot find a path to the controllers.
mod_rewrite seems to be working, because if I add anything plaintext to the .htaccess file, I get a server error 500, and furthermore the index.php rewrite is working. I also have rewrite_short_tags set to TRUE.
I have tried all of the uri_protocol types in the config.php, and none seem to make any difference, so I feel like if it is something i've done, i'm either missing something from routes.php, or something missing from my .htaccess.
Any thoughts would be appreciated. Like I said, this entire setup works on my WAMP server without issue.

I think the problem is in your htaccess. Try this htaccess for a moment..
<IfModule mod_rewrite.c>
RewriteEngine On
# Set the rewritebase to your CI installation folder
RewriteBase /sandbox/ci/
# Send everything to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I hope this helps you out.

Related

Codeigniter Routing not working on Production Environment

I have codeigniter running fine on my local machine but when I push to the server the routing falls apart. For my set up codeigniter runs from a sub directory named 'secure', so this is the .htaccess file I have created in the 'secure' directory:
RewriteEngine on
RewriteBase /secure/
RewriteRule ^assets/less/icons/(.*)$ /secure/assets/icons/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
The routes file currently looks like this:
$route['logout'] = "secure/logout";
$route['passwordReset'] = "secure/PasswordReset";
$route['default_controller'] = 'secure';
On my local set up if you were to go to the URL '/secure/dashboard' controller 'Dashboard.php' is correctly called and index initiated. However if I go to the same URL on the server it is trying to call the controller 'Secure.php' instead of 'Dashboard.php'.
I'm not sure what is different between my local environment and the server thats stopping the server ignoring the 'secure' part of the URL, I thought that this line in the .htaccess file should sort out the sub directory: 'RewriteBase /secure/'.
Any feedback is very welcome right now :/
I finally got to the bottom of the issue. My server holds a few different environments for development which do not run directly from the http root directory. This was causing the variable $_SERVER['SCRIPT_NAME'] to equal '/staging/secure/index.php' instead of '/secure/index.php';, so I simply added the following to the top of the index.php file
$_SERVER['SCRIPT_NAME'] = '/secure/index.php';

CodeIgniter upgrade from 2.2.0 to 3.0.x: Only the default controller is loading

I don't usually post messages on the web to get help but I feel that I have tried everything and I am stuck.
I wanted to upgrade CodeIgniter from version 2.2 to version 3.0.3 (I tried v3.0.2 too) and I followed the corresponding guide on codeigniter.com. But now, only the default controller is loaded, no matter the url.
I have searched on the web and I have found out that it could come from:
the htaccess
the application/config/config file
the application/config/routes file
the new Ucfirst rule for the filenames (controllers, models...)
But I still don't have the solution...
The files in "applications/controllers" are all Ucfirst.
Everything was working fine before (with version 2.2), so WAMP should be correctly configured (mod_rewrite enabled etc...). Moreover, I have tried and succesfully loaded a clean instance of CodeIgniter 3.
Models, Controllers and Views are loaded correctly, I can change my default controller and it will load the correct view (with requests done by controllers and models). But it only loads the default controller, no matter what the url is (in the browser). Even if it does not exist, I never have the 404 page. Except if I specify an incorrect "default_controller" (in the file "routes.php").
It has to be a routing issue...
Here is some of the tests I have done:
Table of tests
Here is my htaccess file (I have tried other ones on the web too, the result is the same):
RewriteEngine On
#Checks to see if the user is attempting to access a valid file,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Enable access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
Here is my "application/config/routes" file:
$route['default_controller'] = 'dashboard';
$route['404_override'] = 'auth/e404';
$route['translate_uri_dashes'] = FALSE;
Any help would be appreciated :-)
Thank you !
SOLUTION
$config['enable_query_strings'] was set to "TRUE" in the file "application/config/config.php". Setting it to "FALSE" solved the issue. So simple...
try putting the index method in the routes, for example instead of this
$route['blog'] = 'blog';
try this
$route['blog'] = 'blog/index';
and if that doesn't work, can you confirm you have set the correct base url in application/config/config.php ?
In codeigniter 3 wamp
I use this htaccess
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
With your error 404 over ride can not be in sub folder.
Change
$route['404_override'] = 'auth/e404';
To
$route['404_override'] = 'e404';
And on config.php
$config['base_url'] = 'http://localhost/your_project/';
// You can leave base url blank but not recommended because when you try to submit forms make cause issue.
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

Removing index.php from Code Igniter

I've done a search for my question but have yet seen one similar to mine.
I have a newly installed CI on my server, for security reason I have shifted the "application" and "system" folder outside of public_html and into a folder called "app". I tried a lot of methods that the forums were using but none seem to help.
Below is my folder structure, would like to know what to type for the .htaccess and where does this .htaccess go to?
/
app (system and application folder for CI resides here)
public_html (index.php - the one which defines ENVIRONMENT, styles, other htmls and my current .htaccess resides here)
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
Please help! Thank you for your time!
EDIT
Currently my url is something like this
xxx.xxx.xxx.xxx/~app/index.php/user/check/id
(it's on a shared hosting but not linked to domain yet and hence the ip address and partition in the url.)
I want to achieve something like
xxx.xxx.xxx.xxx/~app/user/check/id
and it should work when I link up my domain. (e.g. www.mydomain.com/user/check/id)
Alright, I think I've solved the problem.
This has most probably got to do with the 'unique' url that I have. I assume if it is a normal domain name it shouldn't have so much problems.
My url > xxx.xxx.xxx.xxx/~app/index.php/user/check/id
My directory structure remains the same >
/app (application and system directories goes here)
/public_html (index.php and .htaccess goes here)
.htaccess code as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~app/ << NOTE the addition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
In config.php, set
$config['base_url'] = 'http://xxx.xxx.xxx.xxx/~app/';
$config['index_page'] = '';
That's all!

CodeIgniter cannot find anything other than default_controller

I am very new to CodeIgniter so obviously I have problem with the most basic things. I have recently tried to implement a website on local environment. I am using WAMP and the website is in www/public_html. Now the website is working on my localhost but if I try to navigate to any other page from the default (default_controler) it returns not found like
The requested URL /public_html/signin/login/ was not found on this server
I assumed that the website was designed to resolve its path related problems by itself. I am obviously missing something very basic.
I have changed the base_url in the config.php file by the way.
$config['base_url'] = 'http://localhost/public_html/';
I have no clue where else the base_url are or where to change it. Any help will greatly appreciated.
Zain
You are getting this problem because you don't have your .htaccess file configured correctly. At the document root in your .htaccess file you need the following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
In your config.php you will need to do
$config['index_page'] = '';
You also need to make sure you have mod_rewrite enabled
ther is a problem with your index.php. check this line
$config['index_page'] = 'index.php';
if it works u have to add an ht access file to remove index.php from ur url
there are a lot of code for .htaccess file on web try using one that suits you

CodeIgniter htaccess and URL rewrite issues

I have never used CodeIgniter before, let alone ANY php framework and I thought I would give it a try. Everything is going fine except I cannot seem to remove the index.php from the URL and still access my pages.
I have never used the MVC structure so I am learning as I go, so forgive me if I'm doing this wrong.
I am trying to access a view I created called 'about_page.php' by simply typing in localhost/ci/about but currently I can only access it by using localhost/ci/index.php/about
The controller for the page is: /application/controllers/about.php
The Model for the page is: /application/models/about_model.php
And the View for the page is: /application/views/about_page.php
I have searched for a solution to this issue, but haven't been able to find one. Here is where I have already searched:
CodeIgniter - removing index.php
Codeigniter - how to remove the index.php from url?
http://www.farinspace.com/codeigniter-htaccess-file/
CodeIgniter comes with a .htaccess file in the 'application' folder which contains only Allow Deny From All. So I created a new .htaccess file in the root directory, http://localhost/ci/.htaccess and added this code to it:
RewriteEngine On
RewriteBase /ci
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
When the .htaccess file is in the root directory I get a 500 Internal Server Error. When I copy the exact same file into the applications folder the 500 error goes away, but I still cannot access the about page by using localhost/ci/about
I have already changed $config['index_page'] = 'index.php'; to $config['index_page'] = ''; AND I tried changing $config['uri_protocol'] = 'AUTO'; to $config['uri_protocol'] = 'REQUEST_URI'; but I am still getting the Internal Server Error.
I went into the httpd.conf file and uncommented the mod_rewrite.so module so I know mod_rewrite is active.
Does anyone have any ideas why this isn't working or how I can get this work? I know there are alot of questions on StackOverflow on this subject but I couldn't find one that answered my question.
Am I doing this right? Should I even be able to access the about page by visiting localhost/ci/about or do I have to create an 'about' directory in the 'application' directory?
There are 3 steps to remove index.php.
Make below changes in application/config.php file
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
Make .htaccess file in your root directory using below code
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Enable the rewrite engine (if not already enabled)
i. First, initiate it with the following command:
a2enmod rewrite
ii. Edit the file /etc/apache2/sites-enabled/000-default
Change all AllowOverride None to AllowOverride All.
Note: In latest version you need to change in /etc/apache2/apache2.conf file
iii. Restart your server with the following command:
sudo /etc/init.d/apache2 restart
Your .htaccess is slightly off. Look at mine:
RewriteEngine On
RewriteBase /codeigniter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /codeigniter/index.php?/$1 [L]
Notice "codeigniter" in two places.
after that, in your config:
base_url = "http://localhost/codeigniter"
index = ""
Change codeigniter to "ci" whereever appropriate
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dmizone_bkp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /dmizone_bkp/index.php?/$1 [L]
</IfModule>
This works for me
Move your .htaccess file to root folder (locate before application folder in my case)
RewriteEngine on
RewriteBase /yourProjectFolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Mine config.php looks like this (application/config/config.php)
$config['base_url'] = "";
$config['index_page'] = "index.php";
$config['uri_protocol'] = "AUTO";
Let me know if its working for you guys too ! Cheers !
I am using something like this - codeigniter-htaccess-file, its a good article to begin with.
leave the .htaccess file in CI root dir
make sure that mod_rewrite is on
check for typos (ie. controller file/class name)
in /application/config/config.php set $config['index_page'] = "";
in /application/config/routes.php set your default controller $route['default_controller']="home";
If you are running clean installation of CI (2.1.3) there isn't really much that could be wrong.
2 config files
controller
.htaccess
mod_rewrite
read
Codeigniter .htaccess
redirect-index-php-in-codeigniter
For those users of wamp server, follow the first 2 steps of #Raul Chipad's solution then:
Click the wamp icon (normally in green), go to "Apache" > "Apache modules" > "rewrite_module". The "rewrite_module" should be ticked! And then it's the way to go!
if not working
$config['uri_protocol'] = 'AUTO';
change it to
$config['uri_protocol'] = 'PATH_INFO';
if not working change it to
$config['uri_protocol'] = 'QUERY_STRING';
if use this
$config['uri_protocol'] = 'ORIG_PATH_INFO';
with redirect or header location to url not in htaccess will not work you must add the url in htaccess to work
I had similar issue on Linux Ubuntu 14.
After constant 403 error messages in the browser and reading all answers here I could not figure out what was wrong.
Then I have reached for apache's error log, and finally got a hint that was a bit unexpected but perfectly logical.
the codeIgniter main folder had no X permission on folder rendering everything else unreadable by web server.
chmod ugo+x yourCodeIgniterFolder
fixed first problem. Killed 403 errors.
But then I started getting error 500.
Culprit was a wrong settings line in .htaccess
Then I started getting php errors.
Culprit was same problem as main folder no X flag for 'others' permission for inner folders in application and system folders.
My two cents for this question: READ Apache Error.log.
But, also be aware of security issues. CodeIgniter suggests moving application and system folder out of web space therefore changing permissions shall be taken with a great care if these folders remain in web space.
Another thing worth mentioning here is that I have unzipped downloaded CodeIgniter archive directly to the web space. Folder permissions are likely created straight from the archive. As given they are secure, but folder access issue (on unix like systems) is not mentioned in CodeIgniter manual https://codeigniter.com/user_guide/installation/index.html and perhaps it shall be mentioned there with guidelines on how to relax security for CodeIgniter to work while keeping security tight for the production system (some security hints are already there in the manual as mentioned beforehand).
Just add this in the .htaccess file:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
By default the below sits in the application folder, move it like suggested into the root dir.
leave the .htaccess file in CI root dir
Open the application/config/config.php file and make the changes given below,
set your base url by replacing the value of $config['base_url'], as
$config['base_url'] = 'http://localhost/YOUR_PROJECT_DIR_NAME';
make the $config['index_page'] configuration to empty as $config['index_page'] = '';
Create new .htaccess file in project root folder and use the given settings,
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|assets|images|js|css|uploads|favicon.png|favicon.ico|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Restart the server, open the project and you'll be good to go.

Categories