How To Create Custom Error Page In Php
Contents |
Not only do they make your website more professional, they can also save you from losing visits to your site. If a visitor sees a generic error custom 404 page examples page, they are likely to leave your site. However, if they see custom 404 page template a helpful error page, they may continue to stay because they can simply click a link to custom 404 page wordpress go to another page within your site. Simple configuration To create a custom error page for your domain, add the following line to an .htaccess file located in your domain’s how to create 404 error page in html web directory: ErrorDocument 404 /error.php This redirects browsers that experience a 404 error (Not Found) to the file "error.php" located in the same directory as your .htaccess file. Alternatively, you can use the above line as a template to create separate custom error pages for each error. Status codes The complete (and very long) list of errors is available
Custom 404 Page Apache
here: Status codes Some of the most common errors you'll probably want to make entries for are: 400 – Bad Request 401 – Unauthorized 403 – Forbidden 404 – Not Found 500 – Internal Server Error For example, to catch those errors, you would add the following to your .htaccess file for the domain you'd like to configure: ErrorDocument 400 /error.php ErrorDocument 401 /error.php ErrorDocument 403 /error.php ErrorDocument 404 /error.php ErrorDocument 500 /error.php This forces a browser to redirect to the /error.php file if it encounters any of the status codes above. Setting up the error.php file Then, in error.php, add something similar to the following. This particular example is made for a wiki site: if someone visits http://www.example.com/Foo, then they are redirected to http://www.example.com/wiki/Foo. Anything after the last "/" is assumed to be a wiki article they are trying to reach:
In submit Tutorials Questions Projects Meetups Main Site logo-horizontal DigitalOcean Community Menu Tutorials Questions Projects Meetups Main Site Sign Up Log In submit View All Results By: Etel Sverdlov Subscribe Subscribed Share Contents Contents We hope you find this tutorial helpful.
Custom 404 Page Htaccess
In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more 404 error page html code → 7 How To Create a Custom 404 Page in Apache Posted Jul 10, 2012 89.2k views Apache Why Create a Custom 404 how to redirect 404 error page in php Page A custom 404 page lets you provide a user-friendly website to your visitors even in the midst of an error. Very few users, when presented with a 404, will do more beyond click back to get out of https://help.dreamhost.com/hc/en-us/articles/215840318-Custom-error-pages the mistake. A custom 404 page is a good opportunity to keep them on your site and do more to redirect them to their destination. Setup Before going through this tutorial, you should already have created a custom 404 page and saved it into your website's directory. Implement the 404 Page To edit the 404 page, open up or create the site's .htaccess file. You can create it in a text editor and upload it to your site via https://www.digitalocean.com/community/tutorials/how-to-create-a-custom-404-page-in-apache the FTP server. Keep in mind that the name of the file has to be simply .htaccess. Add the following line to the file, replacing new404.html with the correct new error page name: ErrorDocument 404 /new404.html Save and Exit. Keep in mind that the Apache looks for the 404 page located within the site's server root. Meaning that if you place the new error page in a deeper subdirectory, you need to include that in the line, making into something like this: ErrorDocument 404 /error_pages/new404.html See the 404 Page Now visiting unavailable pages on your site should display your custom 404 page! By Etel Sverdlov By: Etel Sverdlov Upvote7 Subscribe Subscribed Share Hacktoberfest Give back to open source this October Celebrate open source software by contributing to GitHub-hosted open source projects for the chance of getting your own limited-edition Hacktoberfest T-shirt. Learn more about Hacktoberfest Related Tutorials How To Migrate your Apache Configuration from 2.2 to 2.4 Syntax. How To Get Started With mod_pagespeed with Apache on a CentOS and Fedora Cloud Server How To Use the .htaccess File How To Set Up Mod_Rewrite (page 2) How to Create an Intranet with OpenVPN on Ubuntu 16.04 9 Comments Log In to Comment Load This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016 DigitalOcean™ Inc. Community Tutorials Questions Projects Tags Newsletter RSS Distros & One-Click Apps Terms,
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://stackoverflow.com/questions/9577596/php-how-to-make-my-own-404-page-not-found-error and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs http://www.onlamp.com/pub/a/onlamp/2003/02/13/davidsklar.html Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; custom 404 it only takes a minute: Sign up PHP : How to make my own 404 page not found error [duplicate] up vote 13 down vote favorite 4 Possible Duplicate: How can I create an error 404 in PHP? how to make my own 404 page not found error in my site so it looks better than the default when someone custom 404 page accesses url like this www.blablabla.com/index99.php which is doesn't exist this will automatically redirected to my own 404 page thank beforehand php share|improve this question asked Mar 6 '12 at 3:41 Agung Setiawan 4242622 marked as duplicate by j08691, Cheery, Phil, animuson♦, Graviton Mar 6 '12 at 4:09 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. Your web server probably just has a template page you can replace, if it is only to be static output. –Michael Berkowski Mar 6 '12 at 3:42 What web server software are you using? It's fairly easy in most of them. Just search for "custom 404 [web server software]". –Norm MacLennan Mar 6 '12 at 3:43 1 It has nothing to do with php. You have either forward all requests with mod_rewrite (if you are using Apache) to the script which will check the existence of the requested page or setup http server. This is an example for Apache httpd.apache.org/docs
Error Pages with PHP and Apache by David Sklar, coauthor of PHP Cookbook 02/13/2003 Using PHP and Apache, you can turn your "Page Not Found" messages into more than bland error reports. You can serve an alternate page based on the name of the page that was not found, create a page on the fly from a database, or send an email about the missing page to a webmaster. Building a custom error page with PHP and Apache requires two steps. You need to tell Apache to run a PHP program when it encounters a 404 ("Page Not Found") error. And you need to write the corresponding program that takes the appropriate action. Configuring Apache To tell Apache what to do on a 404 error, use the ErrorDocument directive: ErrorDocument 404 /error-404.php This tells Apache to serve up error-404.php in the document root directory when it encounters a 404 error. The ErrorDocument directive can go in Apache's httpd.conf file, but it also works in .htaccess files in individual directories. You can have a site-wide error-handling page or different error-handling pages for different parts of your site. Apache also sets some server variables that the error-handling page can access: Related Reading PHP Cookbook By David Sklar, Adam Trachtenberg REDIRECT_URL: the URL-path that was not found. If a user asks for the nonexistent page http://www.example.com/lunch/pastrami.html, for example, this variable is set to /lunch/pastrami.html. REDIRECT_STATUS: the HTTP response status resulting from the request for the original page. In our case, this is always "404". You can use ErrorDocument with other status codes, though, so if you have one error-handling page for multiple statuses, you can use this variable to determine which error status caused the error-handling page to be loaded. REDIRECT_ERROR_NOTES: a brief description of what went wrong, for example, "File does not exist: /usr/local/apache/docroot/lunch/pastrami.html". REDIRECT_REQUEST_METHOD: the method of the request for the original page, such as GET or POST. If there is a query string in the original request, it is stored in REDIRECT_QUERY_STRING. The error page does not have access to the GET or POST variables via $_GET, $_POST, or $_REQUEST, but cookie variables are still available in $_COOKIE. These REDIRECT variables are available in the PHP superglobal array $_SERVER: $_SERVER['REDIRECT_URL'], $_SERVER['REDIRECT_STATUS'], and so forth. Taking Action The information in the REDIRECT variables can be used to do many different things in response to a requ