How can I redirect a 404 Error in a custom 404 page using Codeigniter?
there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following$config['subclass_prefix'] = 'MY_';
Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here as follows.
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
function show_404($page=''){
$this->config =& get_config();
$base_url = $this->config['base_url'];
$_SESSION['error_message'] = 'Error message';
header("location: ".$base_url.'error.html');
exit;
}
}
Now Error controller will be your error page, where the page will be redirected for 404 error.
How to Custom 404 Page Not Found in CodeIgniter 4
Go to your Routes.php file and find
$routes->set404Override();
Now , If you want to display just an error message then write
$routes->set404Override(function(){
echo "your error message";
});
instead of
$routes->set404Override();
Or
If you want to return a view then write like below:
$routes->set404Override(function(){
return view('your_filename');
});
instead of
$routes->set404Override();
Codeigniter Custom 404 Page for different Controllers
Set up a custom 404 controller in the application/config/routes.php file.
$route['404_override'] = 'my404controller';
Create a My404controller
and handle all the 404 errors in the index()
using $this->uri->segment(0)
.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class My404controller extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index() {
// $this->uri->segment(1) is *original* controller segment before routing.
set_status_header(404); // set 404 header
if ($this->uri->segment(1) == 'home') {
$this->load->view('home/home404');
return;
}
if ($this->uri->segment(1) == 'admin') {
$this->load->view('admin/admin404');
return;
}
if ($this->uri->segment(1) == 'blog') {
$this->load->view('blog/blog404');
return;
}
// If there's no match to existing controller, default to generic 404 page view.
$this->load->view('default404');
}
}
codeigniter 3.0 custom 404 not found error page
You need to set in application/config/routes.php
the following route
$route['404_override'] = 'my404';
Then you need to create a new controller in your controllers directory (application/controllers/)
<?php
class My404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$this->load->view('err404');//loading in custom error view
}
}
And create an err404
view. Thats all!
Refer :Reserved Routes
Codeigniter & PHP - forcing a 404?
show_404()
actually sends the proper headers for a search engine to register it as a 404 page (it sends 404 status).
Use a Firefox addon to check the headers received when calling show_404()
. You will see it sends the proper HTTP Status Code.
Check the default application/errors/error_404.php
. The first line is:
<?php header("HTTP/1.1 404 Not Found"); ?>
That line sets the HTTP Status as 404. It's all you need for the search engine to read your page as a 404 page.
page redirect issue codeigniter : 404 error
Change your .htaccess file. Paste this in .htaccess in your app directory (in same level to applications) and will works :
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And in conf.php:
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].'/stock-v2/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Then use regirect like this without refresh:
redirect(base_url('dashboard'));
Check in http.conf do you allow override for your domain?
<Directory "/var/www">
AllowOverride All
# Allow open access:
Require all granted
</Directory>
Codeigniter 4 - how to show 404 page?
I was looking just now exactly for the same thing.
I found here the solution: CodeIgniter 4 User Guide - Error Handling
In your controller, simply write:
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
Related Topics
Bulk Rename Files in a Folder - PHP
Userland Multipart/Form-Data Handler
Call to Undefined Function Curl_Init() Even It Is Enabled in PHP7
How to Use Curl to Fetch Specific Data from a Website and Then Save It My Database Using PHP
How to Check in PHP That I'm in a Static Context (Or Not)
Change Array Key Without Changing Order
JSON_Encode Not Working with a HTML String as Value
Query MySQL and Export Data as CSV in PHP
Next Business Day of Given Date in PHP
Can't Insert Russian Text into MySQL Database
Search and Replace Value in PHP Array
Get Key of Multidimensional Array
Convert Number into Xx.Xx Million Format
Replace Add to Cart Button with a Read More Linked to Product Page on Shop Pages in Woocommerce 3