Codeigniter return 404 page not found - php

I'm developing web application using Codeigniter. So far I have managed to run the application on my localhost. But when I upload the content unto the web hosting, I can't get it to run.
This is what I've tested so far:
strangely, I can call the controller Welcome.php which is the sample controller from codeigniter, and showing well.
But I can't call any other controller which my default controller eventhough I have set it in routes.php, config.php etc.
Below is my configuration for these files:
routes.php
$route['default_controller'] = 'lokadok';
$route['404_override'] = '';
$route['upload/do_upload'] = 'upload/do_upload';
config.php
$config['base_url'] = (( isset($_SERVER['HTTP_HOST']) && strlen($_SERVER['HTTP_HOST'])>0) ?
'http://' . $_SERVER['HTTP_HOST'] : 'http://www.lokadok.co.id' );
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['url_suffix'] = '.html';
$config['charset'] = 'UTF-8';
and this is the .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/assets|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)\.html$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/assets|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
My default controller is this:
lokadok.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Lokadok extends CI_Controller {
public function index() {
$data['is_login'] = $this->session->userdata(LOGGED_IN);
$specialty = $this->input->post('specialty', '');
$address = $this->input->post('address', '');
$doc_name = $this->input->post('doc_name', '');
if ($specialty == '' && $address == '' && $doc_name == '') {
$this->load->model('model_share');
$data['custom_css'] = array(
'style/custom/lokadok.css'
);
$data['sort_by'] = 1;
$data['pop_specialties'] = $this->model_share->get_specialties(true);
$data['specialties'] = $this->model_share->get_specialties();
$this->load->view('templates/header', $data);
$this->load->view('lokadok_view');
$this->load->view('templates/footer', $data);
} else {
redirect("search?specialty=$specialty&address=$address&doc_name=$doc_name&sort_by=1");
}
}
}
?>
I have been looking for the answer for awhile and still couldn't figure it out. So if anyone can spot the error, I would really appreciate it.
To check on the location site you can go to:
www.lokadok.co.id which I expect to call my default controller.
and try to call http://www.lokadok.co.id/welcome which is strangely running well.
Thank you all, and hope my explanation is clear.

I think the answer lies within your question :
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I think you need to enable mod_rewrite
UPDATED
Find your php.ini in
/etc/php.ini
Or here:
/etc/php/php.ini
/etc/php5/php.ini
Find the line with disable_functions, one of them likely being phpinfo. You'll need to remove phpinfo from the list of disabled functions in disabled_functions=
IF UBUNTU THEN
To enable the rewrite module, run "apache2 enable module rewrite" in terminal:
sudo a2enmod rewrite
You need to restart the webserver to apply the changes:
sudo service apache2 restart

Related

Only default controller is working and the rest is 404 not found

I'm using codeigniter on my wamp and everything is fine but when I upload it on live server the default_controller is working but the other controller are 404 not found or "The requested URL /Sample_controller was not found on this server".
Here is my sample controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sample_controller extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index(){
print_r('Hello World');
}
}
and here's my config.php:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';
$config['allow_get_array'] = TRUE;
and this is my router.php:
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
and my .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www.sampleurl.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond $1 !^(index\.php|assets|user_assets|tmp|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
if I tried this sampleurl.com it's working fine and will direct me to my default controller which is the login but if i tried this sampleurl.com/sample_controller or sampleurl.com/login it will lead me to 404 not found The requested URL /sample_controller was not found on this server. Apache/2.4.7 (Ubuntu) Server at sampleurl.com Port 80
I hope someone can help me on this
I tried changing them to:
$config['uri_protocol'] = AUTO
$config['enable_query_strings'] = TRUE;
and it's still not working.
You need to add a route for that controller to work
$route['sample_controller'] = 'sample_controller';
For every new controller you create you need to have a route for it
According to your code, you may have these 2 issues:
1- As per documentation you are required to make your controller filename first character Uppercase.
2- Try adding a question mark "?" in your .htaccess file's last rule. Also remove the "/" before "index.php" check the code below.
Current code:
RewriteRule ^(.*)$ /index.php/$1 [L]
New code:
RewriteRule ^(.*)$ index.php?/$1 [L]
Just simply change
RewriteBase /folder
This line of code in your .htaccess file to appropriate folder name of your project and it will work fine.

How to configure Codeigniter for HTTPS (SSL)?

I have a Codeigniter application which was developed and tested on the normal http. Now the Apache server has been configured in such a way that all of the pages are stored in a single folder which is SSL enabled. The SSL certificate is in place. When I browse to the website using "https://www" then it redirects to "http://www".
NOTE : I do not need to load specific pages using SSL. I just need all pages to show as HTTPS.
I have tried the proposed solutions suggested on Stack Overflow including :
Modifying the .htaccess file to force HTTPS
Setting the $config['base_url'] = "https://www.yoursite.com/";
When I tried the above method in tandem then Firefox gave the following error :
The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Many Stack Overflow contributors suggest that this should be handled by Apache only. That makes sense to me but forcing HTTPS via .htaccess gives the same error.
Is there a simple way to do this without hooks and SSL Helpers, etc?
This answer is a bit (read: a lot) late, but you can use this hook I made (5 minutes ago), It'll redirect to your HTTPS website and set some headers.
I use it along with this .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
It is easy,
Go to applicaton/config.php and insert the following code:
$base = "https://".$_SERVER['HTTP_HOST']; // (For https)
See line 19 on image.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$base = "https://".$_SERVER['HTTP_HOST']; // HERE THE CORRECT CODE
$base .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base;
Step 1
Setting the $config['base_url'] = "https://www.yoursite.com/";
Step 2
Use and edit this .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Step 1: application/hooks/ssl.php
function force_ssl()
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
}
Step 2: application/configs/hooks.php
$hook['post_controller_constructor'][] = array(
'function' => 'force_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);
Step 3:application/config/config.php
$config['enable_hooks'] = TRUE;
htaccess Changes :
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
'mod_rewrite' module to be installed on your Apache server
Final: restart Apache server.
CI_app\application\config\config.php
$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://". #$_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base_url;
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# Require HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Removing the index.php file - https://codeigniter.com/userguide3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

.htaccess throws a 500 error

I am using CodeIgniter and need to get rid of the "index.php" in the URL.
I copied the .htaccess file from codeigniter tutorial, emptied the $config['index_page'] = '';
and put the .htaccess in the root directory.
Without the .htaccess file it works fine, but ugly: www.example.com/index.php/site/home
For some reason it's not working and just throws a 500's error.
Where is the bug and how can I resolve it?
Thank you in advance
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folders by users.
#Additionly this will allow you to create a System.php controller,
#previously this would not have not been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^{.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
<IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php? and everything works as normal.
# submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
My site controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
echo "default function started.<br/>";
$this->hello();
$data['title'] = 'Home!';
$data['name'] = 'Ilia';
$data['fname'] = 'Lev';
$data['operation'] = 'minus';
$data['val1'] = 5;
$data['val2'] = 7;
echo $data['operation']."<br/>";
$data['result'] = $this->math($data);
$this->viewLoad("home_view", $data);
echo "<br/>";
}
public function hello(){
echo "hello function started.<br/>";
}
public function math($data){
$operation = $data['operation'];
$val1 = $data['val1'];
$val2 = $data['val2'];
$this->load->model("math");
return $this->math->calculate($operation, $val1, $val2)."<br/>";
}
public function viewLoad($whatToView, $data){
try{
$this->load->view($whatToView, $data);
}catch(Exception $e){
echo "There is no such view";
}
}
public function about(){
$data['title'] = "About!";
$this->viewLoad("about_view",$data);
}
}
If this is really part of the .htaccess file, it would cause your problem:
RewriteRule ^{.*)$ /index.php?/$1 [L]
^^^ typo?
Should be:
RewriteRule ^(.*)$ /index.php?/$1 [L]
I too had the same problem but my code was correct and i was using wamp server, then
I found rewrite module is not enabled
so went to apache/conf/httpd.conf and uncommented line
LoadModule rewrite_module modules/mod_rewrite.so
Now its working fine.

500 error thrown due to .htaccess bug [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.htaccess throws a 500 error
I am using CodeIgniter and need to get rid of the "index.php" in the URL.
I copied the .htaccess file from CI tutorial, emptied the $config['index_page'] = '';
and put the .htaccess in the root directory.
Without the .htaccess file it works fine, but ugly: www.example.com/index.php/site/home
For some reason it's not working and just throws a 500's error.
In the log there is a row that repeats itself in every attempt: [Thu
Dec 20 04:01:13 2012] [alert] [client 2.55.118.161]
/home/morro1980/data/www/knight-guard.org/.htaccess:
directive requires additional arguments.
The mod_rewrite is installed and working
Where is the bug and how can I resolve it?
Thank you in advance
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folders by users.
#Additionly this will allow you to create a System.php controller,
#previously this would not have not been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
<IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php? and everything works as normal.
# submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
My site controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
echo "default function started.<br/>";
$this->hello();
$data['title'] = 'Home!';
$data['name'] = 'Ilia';
$data['fname'] = 'Lev';
$data['operation'] = 'minus';
$data['val1'] = 5;
$data['val2'] = 7;
echo $data['operation']."<br/>";
$data['result'] = $this->math($data);
$this->viewLoad("home_view", $data);
echo "<br/>";
}
public function hello(){
echo "hello function started.<br/>";
}
public function math($data){
$operation = $data['operation'];
$val1 = $data['val1'];
$val2 = $data['val2'];
$this->load->model("math");
return $this->math->calculate($operation, $val1, $val2)."<br/>";
}
public function viewLoad($whatToView, $data){
try{
$this->load->view($whatToView, $data);
}catch(Exception $e){
echo "There is no such view";
}
}
public function about(){
$data['title'] = "About!";
$this->viewLoad("about_view",$data);
}
}
The first <ifModule> is not closed, this:
RewriteRule ^(.*)$ index.php?/$1 [L]
<IfModule>
has to be
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
#^
Make sure you have <IfModule></IfModule> matched in your .htaccess.

.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