Codeigniter Routing Issues on AWS EC2 - php

I have been searching the stackoverflow all day for an answer to this and I just can't seem to find one.
I have my Codeigniter app on an AWS EC2 server.
Here is my routes.php:
$route['default_controller'] = 'pages/home';
$route['(:any)'] = 'pages/$1';
EDIT Here is my current routes.php:
$route['default_controller'] = 'pages/home';
$route['about'] = 'pages/about';
$route['contact'] = 'pages/contact';
$route['home'] = 'pages/home';
Here is my Pages.php:
class Pages extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$this->view('home');
}
public function about(){
$this->view('about');
}
public function contact(){
$this->view('contact');
}
public function view($page)
{
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
Here is my .htaccess:
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]
My config.php:
$config['index_page'] = '';
PROBLEM
If I go to (my-url), it uses my default_controller.
If I go to (my-url)/home or /about or /contact, I get a 404 error.
If I go to (my-url)/index.php/home or /about or /contact, I get the default_controller.
I have double checked that mod_rewrite is installed and enabled. I can't seem to figure out why I'm not getting anything. I followed the tutorial and read a bunch of stackoverflow questions and answers to try to solve it, but to no avail.
Please help.

You can not use routes as like that:
$route['(:any)'] = 'pages/$1';
It will not work with all functions as you need.
Just Suggestion:
Use your routes as:
$route['about'] = 'pages/about';
$route['contact'] = 'pages/contact';
$route['view/(:any)'] = 'pages/view/$1';
......
You need different routes for different URL if you use (:any) as first param it will not work properly for all functions.

Try this
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

Note when using APPPATH constant, the path ends with '/' already
try change
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
to
if (!file_exists(APPPATH.'views/pages/'.$page.'.php'))

Related

codeingter3 HMVC 'default_controller' is not working

I am using CI3 HMVC in my project. Now, I am facing problem with routing. I want user to type www.demosite.com and it would automatically call my home module. I do not want to show like www.demosite.com/home. I want to show the url like www.demo.com. for this, I set default controller in application/config/routes, like this, as follows;
$route['default_controller'] = "home";
also in my content module, I added a route folder where I wrote
$route['home'] = 'home';
Here is my .htaccess
AddType text/x-component .htc
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !(index\.php|assets/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This is my home controller
class Home extends MX_Controller{
function __construct(){
parent::__construct();
}
function index($stub=""){
$baseUrl=base_url();
$this->load->helper("url");
echo $this->_showHomepage();
}
}
However, when I run this. I got 404 error. What can i do to solve this problem? Thanks in advance.
HMVC it should be like that
This only takes a method no directories are allowed
so the default controller is under controllers
$route['default_controller'] = 'pages/pages/view';
$route['default_controller'] = 'pages';
index is the default method that is called.
$route['home'] = 'authentication/home/index';
$route['home'] = 'authentication/home';
And than you need to change your .htaccess file to match that urls
URI Routing : https://codeigniter.com/user_guide/general/routing.html#examples
Update :
(defined('BASEPATH')) OR exit('No direct script access allowed');
class Site extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('url');
}
}
I solved it. I moved the Home folder from Modules folder to Controller folder and then, it just started to work

Calling Codeigniter controller method not working

I'm starting to develop with CI, I'm trying to call another controller that I call LOGIN but it gets me the error "NOT FOUND", whereas if I put my controller in default_controller, it works, I already have read the other forum about this problem but it does not solve my case,
class Login extends CI_Controller {
public function index() {
redirect("/welcome/index");
$this->load->view("login");
}
}
Possible solutions
1. Please add .htaccess to the codigniter
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
2. Can create a routing for login controller then you can call directly
3. Add $config['index_page'] = 'index.php'; in config.php
Solution -1
class Login extends CI_Controller {
public function index() {
redirect("index.php/welcome/index");
$this->load->view("login");
}
}
Solution-2
application/config/config.php file you not remove index.php
$config['index_page'] = 'index.php';
change it
$config['index_page'] = '';
and using .htaccess index.php remove.
Reference link: index.php from URL

Codeignite hello world code in docs doent work

I just started using CodeIgniter and I'm stuck at the hello world code in the docs. Each time I type in the name of the blog controller in the url bar and hit enter, I get this error:
404 Error Page Not Found!
localhost/php/CodeIgniter/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
route.php
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
config.php
$config['base_url'] = 'http://localhost/php/CodeIgniter/';
$config['index_page'] = 'index.php';
controllers/Blog.php
<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
How Can I Resolve This?
You have to set a URL string on [route.php] corresponding controller class/method.
$route['default_controller'] = 'blog/index';
$route['blog/(:any)'] = 'blog/show/$1'
You can leave the URL "http://localhost/" referencing "/" as home by default_controller on route.php
$config['base_url'] = 'http://localhost';
Please browse more about .htaccess and friendly URL. If you get 404 error maybe your URL is not friendly.
Friendly:
http://localhost/blog/lorem-ipsum
Not Friendly:
http://localhost/index.php/blog/lorem-ipsum
Documentation: https://www.codeigniter.com/userguide3/general/routing.html

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]

.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