CodeIgniter - Default controller not loading automatically - php

I have a default controller set:
$route['default_controller'] = "Home";
However, when I go to http://mydomain.com/, it says 404 Page Not Found, but when going to http://mydomain.com/Home, the controller loads fine. What could be the problem? I've been wracking my head for hours. My htaccess is posted here if needed. Thanks!

Turns out my problem was somewhat unrelated. I had to rename my default controller php file to lowercase and the controller class name to lowercase and everything started to work. When CI looks for the default controller file, it searches in lowercase for the file; if I name my controller file "Home.php" instead of "home.php," CI misses it on Linux (since Linux file systems are case sensitive).

There is nothing wrong with your routing, the problem is with your htaccess file. Try removing
ErrorDocument 404 /index.php

You have probably some .htaccess problem.
Tray this way:
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
config.php
$config['base_url'] = 'www.homepage.com';
$config['index_page'] = '';
routes.php
$route['default_controller'] = "yourcontrollername";
$route['404_override'] = '';

Without any additional information & code from your configs/controllers, I'd suggest checking config/config.php for these (both should be empty in normal cases):
$config['base_url'] = '';
$config['index_page'] = '';
IIRC, I have seen similar issue and it was related to these config variables. Hopefully this helps you a bit.

I sure this .htaccess solved all ubuntu users...
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]

I have a default controller set:
$route['default_controller'] = "home";
it's working for me.

Related

Remove /?home/ from URL (Remove Controller Name from URL And Leave Only Method) | CodeIgniter

Hi Dear Stackflow Community i am newbie in Codeignite coding so i hope you guys give me the time to fix this error thanks is advance :)
So basically i followed a tutorial here and i disabled index.php from URL that problem was fixed but here is my .htaccess right now and i still have the problem of example.com/?home/signin or example.com/?browse/ Or /?admin/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{QUERY_STRING} ^(.*)i=[^&]+(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
</IfModule>
AddType text/vtt .vtt
But the problem is i followed every single tutorial here in stackoverflow on disabling the name of the controller from URL like /?home/ or /?browse/ and every single controller like /?admin/ show in my URL i want a clean url directly with no controller name or the ? mark in it i tried going to my routes.php to change things in it following tutorials but still same issue remains here is how my routes.php looks like right now
*/
$route['default_controller'] = 'Home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Please help me i attempted every single solution but nothing yet, I want to disable the name of the controller from URL like /?home/ or /?browse/ and every single controller like /?admin/ shows in my URL i want a clean url directly with no controller name or the ? mark my controllers are Home, Admin, Browse, General, Payment, Updater can you give examples with them exactly so i can try please to fix the problem according to my controllers
Modify your route file with this and my advice will be to read more about CodeIgniter routing method
$route['signin'] = '/home/signin';
routing work like this, update the route file as you need
$route['YOUR_METHOD_NAME'] = '/YOUR_CONTROLLER_NAME/YOUR_METHOD_NAME';
Try with this:
this my htaccess
// .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
$route['404_override'] = 'my404';
$route['translate_uri_dashes'] = false;
$route['default_controller'] = '/Home';
$route['home'] = '/Home/index';
$route['user-list'] = '/Admin/users';
$route['setting'] = '/General/account';
$route['payment-paypal'] = '/Payment/paymentByPaypal';
and access this way: http://localhost/ (this accesses a Home controller with the index method)
or http://localhost/payment-paypal (this accesses a Payment controller with the PaymentByPaypal method)

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';

CodeIgniter: 404 Page not found after push for server

I did some apps on my local machine, runs ok, but after send that to the server I am getting 404 error page, I tried all methods but none works, can you help me with that?
a2enmod rewrite is on.
I found in google I need use in the server the same version of my php I have in my localmachine or my app will not run, that is true?
my local:
application/config/config.php
$config['base_url'] = 'http://localhost:8888/app/';
$config['index_page'] = 'index.php';
my server:
application/config/config.php
$config['base_url'] = 'http://example.com/app';
$config['index_page'] = 'index.php';
I tried too:
application/config/config.php
$config['base_url'] = '';
$config['index_page'] = '';
I got stuck on that, can you help me with that?
Try this code in your .htaccess file, you are not providing access for your CodeIgniter Directory.
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ /app/index.php/$1 [L]
Do This Configurations
In your config.php
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
in config/routes.php file
$route['default_controller'] = '';//default controller name
$route['404_override'] = '';
then in .htaccess(this should be place out side of application folder)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The error was: in controllers was like that: user.php so i renamed for User.php, i not understand well that, but fixed online my code.
using user.php run in my local but not in live, if somebody know why, please leave a comment too.
thanks all for the help.
cheers.

How to remove controller name from url making it clean in codeigniter

I have the following url..
http://localhost/ci/site_controller/home
I want to remove site_controller controller from url resulting in..
http://localhost/ci/home
How can I do this in CodeIgniter ?
Note: If you'll ask what I've tried than I've just done searching over Google as I don't know how to use mod_rewrite.
EDIT
I have this in my routes.php
$route['default_controller'] = "site_controller/home";
$route['ci/home'] = 'ci/site_controller/home';
$route['404_override'] = '';
but still not working!
.htaccess
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
ErrorDocument 404 /index.php
You can set your a route for each url:
In your config/routes.php file, just set each page like this:
$route['ci/home'] = "ci/site_controller/home";
This might help you to define a Default Controller for your CodeIgniter project.
https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller
For instance, in your case, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'site_controller';
assuming you currently have your url http://localhost/ci/site_controller/home you can just write an explicit route like below to your /application/config/routes.php
$route['ci/home'] = 'site_controller/home';
or
$route['ci/home'] = 'ci/site_controller/home';
if your controller is namespaced to /application/controllers/ci/
This helped for me. In "routes.php" I added this:
$route['(:any)'] = "default_controller/$1";
try using the routes file to re-map url's
http://ellislab.com/codeigniter/user_guide/general/routing.html
I just found the solution in this link, it works for me: http://ellislab.com/forums/viewthread/148531/
$route['^(page1|page2|page3|page4)(/:any)?$'] = "YOURCONTROLLERNAME/$0";
Hope it helps you :)
In the case that your .htaccess file is set up like this (or similar), so index.php is already removed...
RewriteEngine On
RewriteCond $1 !^(index\.php|application|assets|images|js|css|uploads|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And your codeigniter code lies directly in the html folder...
To remove the controller name from URL, edit application/config/routes.php
Add lines like these:
$route['signin'] = 'web/signin';
The line above calls function signin() of controller Web when user requests yoursebsite.com/signin
To pass variables into said function, do the following:
$route['p/(:any)'] = 'web/p/$1';
This line calls function p() in controller Web, which has a parameter. Function p() was defined as:
public function p($param = "none") {
echo $param;
}
I hope this helps :)
It's so simple if you want to remove the controller name from URL in the Codeigniter 3.x
in my case URL was: http://localhost:8080/index.php/page/sometext
Where "Page" is the controller name
open application/config/routs.php
$urlParam = $this->uri->segment_array()[1];
$rout[$urlParam] = "page/index";
Result: http://localhost:8080/index.php/sometext
I assure you, it will work. tested 100%.
Drop the 'ci' from your routes. That is your project name and is never required. Routes always start on the controller level:
$route['home'] = "site_controller/home";
Will work. However, more importantly.. are you sure your design is correct? Why not just let home be a controller? Create a /application/controllers/home.php and you'll be able to access it's index() function with http://localhost/ci/home.
You're complicating things. No need for routes at all.

.htaccess not acting as suspected

I'm using CI and WAMP on a Windows 7 machine. In my Application folder of the site I have a .htaccess file with the following:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
This was copied and modified CodeIgniter User Guide. And I made sure the rewrite_module (didn't it used to be mod_rewrite?) was active and restarted WAMP. In my controller, I have the following:
<?php
class Forum extends CI_Controller
{
public function index()
{
$data['title'] = "Card Forums";
$this->load->view('Header', $data);
$this->load->model('forumdb');
$data['sections'] = $this->forumdb->getSections();
$this->load->view('Home', $data);
$this->load->view('Footer');
}
public function site()
{
$data['title'] = "Card Forums - Site";
$this->load->view('Header', $data);
$data['section'] = "Site";
$this->load->view('Forum', $data);
$this->load->view('Footer');
}
}
When I go to localhost/forum it does exactly as I want. But when I try to go to localhost/forum/site it is displaying the following:
Not Found
The requested URL /forum/site was not found on this server.
In the view Forum I have:
<?php echo $section; ?>
Which is only supposed to display the the variable right now because I want to make sure I'm accessing the page first (just started writing this site Friday). When I try localhost/forum/index.php/site it's giving me a 404 error.
I have a feeling I'm missing something VERY simple here and it's bugging me. Any help would be appreciated.
Here is an updated .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|css)
RewriteRule ^(.*)$ index.php/$1 [L]
In your /forum/config folder find the config.php and change this line
$config['index_page'] = 'index.php';
To
$config['index_page'] = '';
Then as you mentioned you can use function like base_url('css/site.css');
One thing I generally do with this type of htaccess file is add an entry for a folder called public so in my case it would look like this.
RewriteEngine on
RewriteCond $1 !^(index\.php|public)
RewriteRule ^(.*)$ index.php/$1 [L]
Then I have all my assests in the public folder, and I don't need to keep adding exceptions to my htaccess file.
For WAMP (and most other environemnts for my CI projects), I've had most luck with the following additional flags in .htaccess:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]
Also, Check your uri_protocol in config.php. I've had most luck with setting it to PATH_INFO instead of AUTO.
One last thing to try: CI sometimes detects the base_url wrong in WAMP environments. Either define your base_url in config.php.

Categories