How to Make Custom 404 "Not Found" Page In Codeigniter?

6 minutes read

To create a custom 404 "not found" page in CodeIgniter, you can follow these steps:


Create a new view file called "404.php" in the "application/views/errors" folder of your CodeIgniter project.


Inside this view file, you can design the layout and add any content you want to display on the 404 page.


Open the "application/config/routes.php" file and add the following line to set the custom 404 page:


$route['404_override'] = 'errors/error_404';


Create a new controller file called "errors.php" in the "application/controllers" folder of your CodeIgniter project.


Inside this controller file, create a method called "error_404" which will load the custom 404 view file:

1
2
3
4
public function error_404()
{
    $this->load->view('errors/404');
}


Now, when a page is not found on your website, CodeIgniter will automatically redirect the user to your custom 404 "not found" page.

Best CodeIgniter Cloud Hosting Providers in November 2024

1
DigitalOcean

Rating is 5 out of 5

DigitalOcean

2
AWS

Rating is 4.9 out of 5

AWS

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.7 out of 5

Cloudways


What is the relationship between SEO and a custom 404 page in CodeIgniter?

SEO and a custom 404 page in CodeIgniter are related in the sense that having a well-designed and functional custom 404 page can help improve the overall SEO performance of a website.


When a user navigates to a broken or non-existent page on a website, they are typically presented with a standard 404 error page. However, creating a custom 404 page in CodeIgniter allows you to provide a more user-friendly and helpful experience for visitors who encounter a broken link or mistyped URL.


By customizing the content and design of your 404 page, you can keep users engaged, direct them to relevant content on your site, and encourage them to continue exploring. This can help reduce bounce rates and improve user experience, which are important factors that search engines like Google take into account when ranking websites.


Additionally, by including links and other relevant information on your custom 404 page, you can help search engine bots discover and index important pages on your site that may otherwise be overlooked if they are only accessible through broken links.


In summary, having a well-designed custom 404 page in CodeIgniter can improve user experience, reduce bounce rates, and ultimately contribute to better search engine rankings and overall SEO performance for your website.


What is the ideal content to include on a custom 404 page in CodeIgniter?

  1. A friendly and engaging message apologizing for the error and reassuring the user that the issue will be fixed.
  2. Suggestions or links to helpful resources such as the homepage, a search bar, or popular pages on the website.
  3. Contact information or a link to the support team for assistance.
  4. A search bar or navigation menu to help the user easily find what they were looking for.
  5. Links to related content or popular pages to help the user continue browsing the website.
  6. A humorous or creative image or illustration to make the error page more visually appealing and engaging.
  7. Custom branding or design elements to ensure the error page matches the overall look and feel of the website.


How to handle 404 errors in CodeIgniter?

In CodeIgniter, you can handle 404 errors by creating a custom 404 page and setting it up in the configuration file. Here's how you can do it:

  1. Create a custom 404 error page: Create a new PHP file in your views folder (e.g., application/views/errors/html/error_404.php) and add your custom message or content for the 404 error page.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<!DOCTYPE html>
<html>
<head>
    <title>404 Page Not Found</title>
</head>
<body>
    <h1>404 Page Not Found</h1>
    <p>The page you are looking for could not be found.</p>
</body>
</html>


  1. Update the configuration file: Open the application/config/config.php file and set the custom 404 page in the config array.
1
2
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_-';
$config['error_view'] = 'errors/html/error_404.php';


  1. Handle 404 errors in the controller: You can also handle 404 errors in the controller by checking if a certain page or resource is not found and then showing the 404 error page.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public function show_page($page)
{
    // Check if the page exists
    if (! file_exists(APPPATH.'views/pages/'.$page.'.php'))
    {
        // Show the custom 404 error page
        show_404();
    }

    // Load the requested page
    $this->load->view('pages/'.$page);
}


By following these steps, you can handle 404 errors and show a custom 404 page in CodeIgniter when a page or resource is not found.

Facebook Twitter LinkedIn Telegram

Related Posts:

To create a custom 404 error page in Next.js, you can follow these steps:In your Next.js project&#39;s root directory, navigate to the pages folder.Create a new file named 404.js. This file will represent your custom 404 error page.Inside the 404.js file, you ...
You can find the XAMPP default 404 page files in the &#34;htdocs&#34; directory of your XAMPP installation. The specific file is typically named &#34;404.html&#34; or &#34;404.php&#34; and can be customized or replaced with your own design or content. This pag...
Creating a custom 404 error page in WordPress allows you to design a more user-friendly and personalized page for visitors who encounter broken or unavailable links. Here&#39;s how you can do it:Access your WordPress dashboard by logging in as an administrator...