Subdomain does not work for CodeIgniter - php

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

Related

Duplicate content issue due to Codeigniter controllers

I am assisting with a Codeigniter website and don't have a background with it. It has been noted that there is duplicate page content. When I dug in, they are not actual pages, but controllers pointing to the same views. I attempted to set up 301 redirects in an .htaccess file in the root of the website, but it is not taking. In the application/config/routes.php, the following is noted:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
For example, there is a page http://mywebsite.com/weddings, and then http://mywebsite.com/Home/weddings was another controller that pointed to the same view. I attempted to add a 301 redirect from Home/weddings after removing the additional controller pointing to it, but now Home/weddings just redirects to the home page, as it is set up with the 404 override. I hope I have provided enough information for some assistance.
.htaccess file:
RewriteEngine On
RewriteBase /
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# allow access to certain directories in webroot
RewriteCond $1
!^(index\.php|robots\.txt|css/|lib/|js/|images/|^(.*)/images)
# gets rid of index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# page redirects
RedirectMatch 301 ^Home/weddings/$ /weddings
Home.php is as follows:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Admin_model');
$this->load->model('Home_model');
$this->load->helper('captcha');
$this->load->library('recaptcha');
}
public function weddings()
{
$this->load->view('Template/Home/front_header');
$this->load->view('Home/weddings');
$this->load->view('Template/Home/front_footer');
}
Do not touch .htaccess. You have two options.
Create a controller Weddings.php and in your index.php return a
view for weddings page.
The other option is to use Home.php controller and then add weddings method, the method should return the weddings page view. Your route should look like below:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
$route['/weddings'] = 'home/weddings';
Hope this will help. Thank you
Edit
In your Weddings.php controller constructor check if there exist a "Home" and return 404 error. See below
function __construct()
{
parent::Controller();
if($this->uri->uri_segment(1) == 'Home') {
show_404();
}
}

Cannot access CI Controller

I have a little problem with a tiny CodeIgniter app developed by another guy who had my job a while back. The app is already implemented, I just have to modify it a bit. The problem is that I can only access the two controllers that are already created. If I create another controller and I try to access it I get a 404 error.
Controller name: Test.php
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function index()
{
die('test');
}
}?>
Result when trying to access it by myapp.com/test or myapp.com/index.php/test or myapp.com/index.php/test/index:
404 Page Not Found. The page you requested was not found.
Config/routes.php:
$route['default_controller'] = 'pages';
$route['brand'] = '/';
$route['brand/(:any)/(:any)'] = 'brands/brand/$1/$2';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Pages.php and Brands.php are the two controllers already implemented.
.htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
If I try to access via url one of the other two controllers it works just fine. But I need to create new controllers for the changes I have to implement. What should I do? Where should I look for problems?
Thank you!

calling function from view in codeigniter not working

I am calling controller function from view with base url it's going to that function but showing 404 Page Not Found.
.htaccess I am using is this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
config.php
$config['index_page'] = '';
autoload is
$autoload['helper'] = array('form', 'url', 'security');
default controller in routes.php is
$route['default_controller'] = 'prog_bar';
base_url is
$config['base_url'] = 'http://localhost:8080/jsLearning/prog_bar/';
view
a href="<?php echo base_url().'file_func'; ?>">abc</a>
controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class prog_bar extends CI_Controller {
public function index()
{
$this->load->view('prog_bar_view');
}
public function file_func(){
echo "form";
}
}
what else I am missing?
Class name should be started with capital letter,
Change this
class prog_bar extends CI_Controller {
to
class Prog_bar extends CI_Controller {
Your base_url is wrong. It should be
$config['base_url'] = 'http://localhost:8080/jsLearning/';
and now in HTML use site_url() instead base_url() like
abc
base_url() should be used only for resources link like image file, css file etc
Update your .htaccess by following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I think you should be great with settings below:
In Config.php:
$config['base_url'] = 'http://localhost:8080/jsLearning/';
In Your View:
abc
In Your Controller:
class Prog_bar extends CI_Controller {
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you have still error, check to see what your base_url link to..Create a link
Base Url
to see in your browser where it links.
You do not use the base url for links within the site. Use the controller name and the methods name
<?php echo anchor("Pages/entry", "Post Entry"); ?>
What is form? It is not defined. Is it a page? if so, you have no view address for it. If its a snippet, you still need an address
$this->load->view('prog_bar_view')
a function will not simply show a view or snippet without being told where it is. If you are trying to include a form in a page, then just use nested views and in your main(html) view load the view you are trying to show with $this->load->view()

Why this controller is not working in code-igniter version 3.0.1?

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.

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

Categories