htaccess 404 include page - php

I know I can redirect to a 404 page with htaccess, but can I just include the custom 404 page I have into the URL that is causing the 404? For example, someone types in blahwat.php, which is not on the server, can I keep them on that page but instead of the apache 404 can I have it include my custom 404 page?

Using ErrorDocument does not redirect, so I don't know why you're bringing redirection up.
In any case, if there was a page at the URL then you wouldn't be getting a 404 in the first place.

Related

Redirect 404 error pages to another host

What I would like to do is to redirect visitors hitting the 404 error page on my main website (say example.com) to another host.
For example:
A visitor hits the page https://example.com/abc which doesn't exist. I would like to redirect him to https://another-host.example.com/abc and so on.
Any idea on how this can be done?
if you are using any framewor, it will have a default routing and 404 handler built in, all you need to do is extend that handler to do a redirect. If that is just a plain code, use apache to do your redirects: Google box responce when searching for apache 404 redirect (applies to internal paged or external domain redirects):
"ErrorDocument 404 /index.html means that when the client access a non exist URL, Apache will redirect to index.html page."
or
In your error_404.php page write header("location:https://another-host.example.com/abc");
or
In your error_404.php page write redirect("location:https://another-host.example.com/abc");
or
$(function(){
location.href = "https://another-host.example.com/abc";
});
first get the error code on routes then you apply your redirect process under condition

Track links that causes 404

I am trying to track all those link that causes 404.i tried $_SERVER['HTTP_REFERER'] on 404 page but it doesn't work for me .
NOTE: i don't wanna use google analytic API. Is there any other possible way
Create a .htaccess file that will redirect to a 404 page on 404 error.
Add this code to your .htaccess file: ErrorDocument 404 /404.php and in the root of your code create a file called 404.php. In that 404.php file you can do all the handling of the error.
$_SERVER['REQUEST_URI'] should have the URL that was requested in the browser.
Also make sure that you are loading the 404 page and not redirecting to the 404 page.

Apache 404 error redirecting one site's error page for all other sites also

I have 3 wordpress sites in my machine(in localhost) named site1, site2, site3. Whenever there is page not found it is being redirected to the site1's 404 error page instead of redirecting to their respective 404 pages. may be I set the apache's default error page somewhere. could anybody help me in this regard.
how to reset it back.
check the .htaccess file in the root of your site, I think in there a line should say something similar with ErrorDocument 404 /errors/404.html
try commenting out that line with a # and refresh

detect the name of the page that the user came from

I am redirecting the pages to 404.php if the page is not found from IIS settings.
My problem is that I would like to detect the LINK that they were looking for.
For example:
if some one access to http://www.site.com/nosuchpage it will redirect to 404.php but using $_SERVER["HTTP_REFERER"] I am not getting url where the user came from.
Please help.
If you are redirecting them to 404.php via htaccess, i.e.
ErrorDocument 404 /404.php
Then you should just be able to use $_SERVER['REQUEST_URI'] as the URL they requested should still be the url that wasn't found.

Problem with apache mod_rewrite and 404 pages

I have a custom 404 error page, and a mod_rewrite rule. If I access a page that does not exist, I get my 404 error page. My problem is that if I issue a 404 header from my php page, it does not open my 404 page, instead, I get this:
Not Found
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying
to use an ErrorDocument to handle the request.
This is my .htaccess:
RewriteEngine on
ErrorDocument 404 /errors/404.php
RewriteRule ^[A-Za-z0-9]{8}/$ /index.php
This is my redirect from /index.php that will 404 only if the key does not exist. The $key is obtained from parsing the URL (e.g. http://localhost/aKeYCoDE/):
<?php
if (!key_exists($key)){
header('HTTP/1.0 404 Not Found');
exit;
}
?>
I am expecting it to redirect to my 404 page.
UPDATE:
It is definitely something about the fact that I am calling 404 from a page that was rewritten (/index.php). If I create a dummy page: /redirect.php, and then do nothing but issue the 404 from there, I get my custom 404 page. But, if I write a mod_rewrite rule for it, and try to access it that way, I get the default 404 error page.
I found the answer - the problem was my assumption that a header 404 from php would redirect to a 404 page. That was wrong. The Apache server can issue 404 for pages that do not exist, but for pages that do exist, i.e., are being served by the php page, the 404 response goes to the browser.
This thread was of a similar issue:
http://www.webmasterworld.com/forum88/10955.htm
To make php do what I want it to do (issue the 404 when key does not exist), I need to include the 404 page from php:
<?php
if (!key_exists($key)){
include($_SERVER["DOCUMENT_ROOT"]."/errors/404.php");
exit;
}
?>

Categories