Php Redirecting To Error Page
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with how to redirect 404 error page to homepage in php us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack
Php Redirect To 404 Error Page
Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up php header 404 redirect PHP - Display a 404 Error without redirecting to another page up vote 8 down vote favorite 5 I want to display a 404 Error if a user reaches a page that exists but I don't want him/her to see. I set error handler don't want to do redirect (that would cause the address bar to show the link of the error page in the address bar) like the following: if ($this_page_should_not_be_seen) header("Location: err.php?e=404"); Instead, it should seem like the page really doesn't exist, without having the URL in the browser's address changed. php custom-error-pages share|improve this question edited May 31 '15 at 8:29 asked Jun 21 '12 at 11:57 Dilip Raj Baral 1,16851542 1 and what's wrong with htaccess? –k102 Jun 21 '12 at 11:58 1
Htaccess Redirect 404
That's the job for .htaccess, not for PHP file... or, for both of them, if you just redirect everything to index.php, but still, you need .htaccess or access to httpd.conf. –Griwes Jun 21 '12 at 11:59 1 you mean your browser shows the 404 error rather than a custom 404 page? you can redirect it to some page with doesn't exist in actual. –Ummar Jun 21 '12 at 11:59 @Ummar That would work, but I was wondering if there is formal way to do this.. –Dilip Raj Baral Jun 21 '12 at 12:15 1 best formal way I think is .htaccess –Ummar Jun 21 '12 at 12:20 | show 1 more comment 5 Answers 5 active oldest votes up vote 20 down vote accepted Include the error page in your current page and send a 404 error status code: Note that this should be used if the page should never be seen. A better status code for un-authorized access (if the page should be seen by some logged in users) is 403 (Not Authorized). share|improve this answer edited May 31 '15 at 9:18 Dilip Raj Baral 1,16851542 answered Jun 21 '12 at 11:59 Emil Vikström 59.4k1183
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about htaccess 404 Stack Overflow the company Business Learn more about hiring developers or posting ads php redirect to another page with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
404 Page
is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Best way to handle 404 error with redirect to homepage up vote 1 http://stackoverflow.com/questions/11137625/php-display-a-404-error-without-redirecting-to-another-page down vote favorite I've searched, read a lot, but still couldn't find a way, to achieve my goal. Now I even think it is impossible. Is it? I want 404 errors to handled properly so I have ErrorDocument 404 /404.php in the .htaccess file, and I want to send out the right header to search engines header("HTTP/1.1 404 Not Found") to forget that page, also I want to redirect my http://stackoverflow.com/questions/11501985/best-way-to-handle-404-error-with-redirect-to-homepage visitor to /index.php?code=404 so I can inform him about that what he was looking for is not available, but still keep him on the site. Now it seems everything is fine, until I want to redirect the user: then the server sends out a 302 message, which overwrites my previous 404 header, so while I can send the user to the desired place, I still can't tell the searchbots that the address is wrong. If I add a include("index.php?code=404") after the header line it just won't work, the server says that file does not exist. What should I do? php html apache share|improve this question asked Jul 16 '12 at 9:54 ZTefter 8419 I've forgot to mention: as I searched on the topic, it raised an interesting question: what if my index.php?code=404 page has something missing, like an icon for example. While my site generates the page, the icon will give a 404, which will redirect, which will generate a 404... so isn't there a chance that my original idea will generate some infinite loop? –ZTefter Jul 16 '12 at 10:11 add a comment| 3 Answers 3 active oldest votes up vote 5 down vote accepted Try something like this:
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 https://help.dreamhost.com/hc/en-us/articles/215840318-Custom-error-pages page, they are likely to leave your site. However, if they see a helpful error page, they may continue to stay because they can simply click a link to 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 error page 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 404 error page 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: