Codeigniter routing issues - php

I am customizing my routes in codeigniter, here is a sample of my routes.php:
$route['default_controller'] = 'maincontroller/main';
$route['404_override'] = '';
$route['test'] = 'maincontroller/test';
here is the maincontroller class:
class Maincontroller extends CI_Controller
{
public function __construct( )
{
parent::__construct( );
$this->load->library('session');
$this->init( );
}
public function main( )
{
echo "in main";
}
public function test( )
{
echo "test";
}
}
but when i access this url in my browser: /test, i get the server's 404 page not found error.
I have no idea what im doing wrong, i followed the routes guide in codeigniter user guide.

Instead of going to /test , try going to /index.php/test
By default CodeIgniter runs everything through index.php.
You can change this behaviour by adding a .htaccess file to the site root folder.
This is taken straight from the CodeIgniter user guide.
https://ellislab.com/codeigniter/user-guide/general/urls.html
The htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
If you do this you also need to change your config/config.php file
find
$config['index_page'] = 'index.php';
change it to
$config['index_page'] = '';
When you want to create links in a page (view) you can use the built in site_url() function, you may need to load the url helper for this.
for example
<?php echo site_url('test'); ?>
This will take into account the $config['index_page'] setting and create the right url for your link.

if you are trying to run your site from localhost use the below code..
adding a .htaccess file to the site root folder (e.g: mysite).
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /root folder/index.php/$1 [L]
for the site hosted on the remote hosting server, use this code
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Related

How do I use codeigniter in a subfolder on a server?

I have two codeigniter installations under my public_html folder on my server. One is directly under public_html and the other is under public_html/posts_receive.
The instance under public_html is working normal but when I try to access the codeigniter controller inside public_html/posts_receive it shows:
404 Page Not Found
tried url http://www.example.com/posts_receive/index.php/dashboard
To solve this problem I have added an .htaccess file under public_html/posts_receive which contain the following:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /posts_receive
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
My controller is named dashboard.php and contains the following:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library("pagination");
}
public function index() {
$config = array();
$data['link_sel']='dashboard';
$data['stats']=$this->fetch_stats();
$this->load->view("header_inc", $data);
$this->load->view("dashboard", $data);
$this->load->view("footer_inc", $data);
}
}
?>
config.php includes:
$config['base_url'] = 'http://www.example.com/posts_receive/';
$config['index_page'] = 'index.php';
You should run both applications from one CodeIgniter base. You will have 2 sub folders in your application folder. Take a look at the documentation: https://www.codeigniter.com/userguide3/general/managing_apps.html
From there on you can create the same index.php file as in your root in your posts_recieve folder and point it to the right application directory. That is how i managed to do it. Otherwise the CodeIgniter instance in your root will think you are navigating to a controller called posts_recieve which does not exists in that application.Hope this helps. Otherwise, just ask when things are unclear.
Inside .htaccess, RewriteBase /posts_receive part is not needed.
RewriteRule ^(.*)$ index.php?$1 [L]
should become
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
(<IfModule mod_rewrite.c> is a server configuration condition. It means you should have mod_rewrite module installed and active on the web server for these settings to work.)
You could leave empty $config['index_page'] inside config.php:
$config['index_page'] = '';
And you can call the controller without the index.php part:
http://www.example.com/posts_receive/dashboard

Subdomain does not work for CodeIgniter

I've a Subdomain under a maindomain. I want to install CodeIgniter under subdomain. But subdomain does not work for CodeIgniter default route.
Like my subdomain is demo.example.com not working. Normally core php is working for this subdomain but CodeIgniter not.
But when I call demo.example.com/welcome then working. Here welcome is a Controller name.
Can anyone help me what is my wrong ?
My Base URL is:
$config['base_url'] = 'http://demo.example.com';
My .htaccess is
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
My default controller(welcome.php) is
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
echo 'Welcome Subdomain';
}
}
Your controller name is Welcome.php as a public_html/demo/application/controllers/Welcome.php.
Please change instead Welcome.php to welcome.php
Set instead $route['default_controller'] = "Welcome" to $route['default_controller'] = "welcome" in routes.php
If you have CodeIgniter install in your main domain add RewriteBase / in your .htaccess

CodeIgniter setting up routing properly

I'm trying to setting up my codeigniter routing to make an authentication form for an admin panel, but the routing is not working. I'm a beginner with CodeIgniter and I think I'm missing something.
In my server I put the CodeIgniter fiels inside folder in my root directory, so it can be accessed like this:
/localhost/ci
In my config.php I set my base url and remove the index page to remove index.php:
$config['base_url'] = 'http://localhost/ci/';
$config['index_page'] = '';
Also I have edited the .htaccess to remove the index.php like CodeIgniter documentation says, so it looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my routes config file looks like this. I set my default page and a route to my Auth controller:
$route['default_controller'] = 'auth';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['auth/login'] = "auth/login";
This is my Controller:
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
// load libraries
}
function index() {
if (! $this->ion_auth->logged_in()) {
// redirect them to the dashboard page
redirect(base_url('auth/login'), 'refresh');
} else {
// show the dashboard page
redirect(base_url('dashboard'), 'refresh');
}
}
function login() {
$this->load->view('login');
}
When I enter in the web-browser http://localhost/ci/ it redirects me to http://localhost/ci/auth/login, but then a 404 Not Found error ir raised. What am I missing? I'm a bit confused at this point, thanks.
Try changing your .htaccess from:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
To
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

Codeigniter showing 404 Page Not Found on Hostgator

I'm currently testing my Codeigniter Project on my own Hostgator account. The project works well on my WAMP server but it shows a 404 Page Not Found error when online on Hostgator. I've tried playing around with the file permissions on FTP to playing around with the .htaccess file. The current site can be viewed here: www.test.jeffreyteruel.com.
Here's a look at my .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
My current file structure:
-application
-assets (css/js/img files)
-cgi-bin(from the server)
-system
-uploads
-.htaccess
-index.php
Here's my current config.php info:
$config['base_url'] = 'http://test.jeffreyteruel.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
The default controller on the route.php file is: $route['default_controller'] = 'main';
Main Controller (main.php):
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
//insert model here
$this->load->model('main_model');
date_default_timezone_set('Asia/Manila');
}
public function index()
{
$content['main_content'] = 'home/home';
$this->load->view('main',$content);
}
...
?>
Any help to get the site showing properly would be appreciated.
I have CI running on a subdomain on Hostgator and I recall a slight struggle getting it working but as I'm old I've got a memory like a.... Hmmm can't remember!
My Simplified .htaccess is
RewriteEngine On
RewriteBase /
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Addition:
In your config.php
$config['base_url'] = 'http://test.jeffreyteruel.com';
Add the trailing /
$config['base_url'] = 'http://test.jeffreyteruel.com/';
UPDATE:
With Codeigniter 3 All Class names need to be Capitialized - First letter Uppercased ( ucfirst ).
Refer to : codeigniter.com/userguide3/general/styleguide.html#file-naming

.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