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
Related
When I am using url as
mysite.com/index.php/user/user the method is getting called.
But when I add below line in routes.php so that I can access the function using custom url.
it is throwing me error. 404 Not Found
I want to access the method using url as mysite.com/user
$route['user']['get'] = 'user/user';
user.php is present inside controller directory
<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class User extends REST_Controller {
function __construct()
{
// Construct the parent class
parent::__construct();
}
public function user_get(){
$this->set_response("request recieved", REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
}
[2nd question]
how to make a route to access the controller present inside certain folders in controller directory.
suppose a controller file is present in controllers/api/v1/ directory with file name user.php
Note:
I have tried all the solutions given by users on other posts but issue was not resolved.
EDIT: Issue Resloved
Routing is working just fine now. I think the problem was, I was calling mysite.com/user , instead I should have called mysite.com/index.php/user
Second issue of index.php being in the url is also resolved.
I was making the changes in .htaccess file which was present in Application folder instead.
I created a .htaccess file in root folder then added below mentioned line of code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
You have set your route as
$route['user']['get'] = 'user/user';
Which is directing to the user controller and the user method. But in your controller you have a method called user_get.
So the simplest fix here is to change your route to point to the correct method.
NOTE: You have no method called 'user' so why is that in the route?
So this...
$route['user']['get'] = 'user/user'; // the user method does not exist
Would become...
$route['user']['get'] = 'user/user_get'; // the user_get method does exist
Update: To remove index.php from your URL, your .htaccess might be
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And make sure you add in your base_url in applications/config/config.php
$config['base_url'] = 'http://example.com'; // Change to your sites URL
$config['index_page'] = ''; // remove index.php
class Users extends CI_Controller{
public function index(){
echo 'hello';
}
}
I kept the file name as Users.php, still its not working.
Please Consider about Codeigniter naming conversion.
and
in config.php
$config['base_url'] = '';
$config['index_page'] = '';
place .htaccess outside application folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
and
in routes.php
$route['default_controller'] = "users";//default controller name
and in URL
http://localhost/<path to file>/project_name/users
Note : You cant access URL with any file extensions like .php or .html.
start contract function in your class
class Users extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
echo 'hello';
}
}
And run the url as example.com/index.php/users/
This might help you out:
-> app/config/routes.php
$route['default_controller'] = 'pages/homePage';
-> app/controllers/Pages.php (Note the capital P on the file)
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends MY_Controller {//Note the capital Class name
public function homePage()
{
$this->load->view("pages/homepage.php");
}
}
Also to get rid of the index.php do the following :
-> app/config/config.php
$config['index_page'] = '';
Create a file called ".htaccess" outside the app folder and put the following code in it:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This HTaccess is from another PHP framework called Laravel, it seems to be working better than the standard CI's HTaccess that they provide on their website.
I hope this helps you out.
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
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.
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]