Capturing and logging broken links on a custom 404 page - apache

I am trying to create a customer 404 page in php that captures and can log into a database the page that was requested. I have this in my .htaccess file already:
ErrorDocument 404 http://mywebsite.com/notfound.php
What I am trying to do is append the requested URI to as a query string to the URL. I have tried this:
ErrorDocument 404 http://mywebsite.com/notfound.php?url=%{REQUEST_URI}
but the URL shows the literal text that I put in above. So I figured I would need some sort of flag at the end like [QSA] or something but all attempts result in a server error 500.
My ultimate goal is to capture the attempted broken link and the referring URL (which I can get from $_SERVER['HTTP_REFERER']) on the notfound.php page and log this into my database where I can pull a report of all links, hyperlinked or called via AJAX, that are broken or missing. I am able to do everything except get the requested URI of the actual link that caused the 404 error.

You shouldn't use ErrorDocument 404 with http:// in the target to avoid full redirect, like this:
ErrorDocument 404 /devsite/notfound.php
Then original 404 URI will be available to you as $_SERVER["REQUEST_URI"]

Related

htaccess - The custom 404 error page is not working properly

EDIT: This question has been solved. Please see my answer.
I have the file .htaccess that contains only the following line (I have added a custom 404 error page)
ErrorDocument 404 404.php
This is the code of the page 404.php:
<?php echo("file not found")?>
The problem is that if i type in my browser for example the url http://127.0.0.1/www/website/this_file_does_not_exist the page shows this line:
404.php
In other words, i see only the filename of the custom 404 error page instead of the content of the 404 error page.
I have already searched on stackoverflow for a solution without success.
How can i fix this problem?
From Apache documentation:
The syntax of the ErrorDocument directive is:
ErrorDocument <3-digit-code> <action>
where the action will be treated as:
A local URL to redirect to (if the action begins with a "/").
An external URL to redirect to (if the action is a valid URL).
Text to be displayed (if none of the above). The text must be
wrapped in quotes (") if it consists of more than one word.
In other words, if you're going to use a local URL it has to begin with a "/":
ErrorDocument 404 /404.php

The requested URL was not found on this server

I have simple html site. how I can fix the problem 'The requested URL was not found on this server' when user gives wrong URL. for example user type mydomain.com/abc.html and this page is not exist on server, so it will cause an error. how to redirect it on error page to avoid this error.
Create a .htaccess file and add this line.
ErrorDocument 404 /foo.html
This will redirect any 404 error to foo.html

Redirect via htacces to error pages (not found & Internal Server error)

can I vía .htaccess redirect when
Users finds an Internal Server Error
A not found page
Is that posible? if so, can someone help me with the rewrite rules?
edit
trying
ErrorDocument 500 /oohps.php
ErrorDocument 404 /where.php
and adding them at domain.com/oops.php and domain.com/where.php but still not loaded
Why not just use custom error responses via ErrorDocument?
ErrorDocument 500 /errors/internal-server-error.html
ErrorDocument 404 /errors/not-found.html
It is useful I am able to redirect to index page on 404 error(page not found) by modifying "ErrorDocument" in /etc/apache2/conf.d/localized-error-pages file.
Thanks.....

forward Request parameters to custom 404 page using ErrorDocument in apache?

Is there a way to have the request parameters "forward on" to the custom error file in apache much like [QSA] does on rewrite rules?
if i send in www.foo.com/doesnotexist?bar=true
and i have this in vhost:
ErrorDocument 404 /customerrorpage
then it would call customerrorpage with:
customerrorpage?bar=true
I know this is really old question, but it hasn't been answered and so in case someone else is searching the site for the answer I'll share what I know.
The Apache 2.2 ErrorDocument page says that when you have a Custom Error page which is a local page then a number of environment vars will be set which can be read from your customer error handler. One of the vars is REDIRECT_QUERY_STRING which will contain the query string that the original page had. So in your example the REDIRECT_QUERY_STRING would contain 'bar=true'.

Redirect 404 to another domain with apache?

Hey guys, have a question regarding apache. I have a site that's been re-engineered, but I want to capture all the 'old' links that people may have bookmarked or come from search engines to the old site which is under a new domain name. How do I get apache to redirect only 404 not found to the old site?
TIA,
J
You should first decide what status code you want to send. Sending both a 404 status code and a redirect is not possible.
But seth did already mention the right method, the ErrorDocument directive:
# local path
ErrorDocument 404 /local/path/to/error/document
# external URI
ErrorDocument 404 http://uri.example/to/error/document
If you use a local path, the 404 status code is sent. If you use an absolute URI, a 302 status code (temporary redirect) is sent.
And if you want to send a 301 redirect:
Redirect 301 / http://new.example.com/
Your old domain should capture all responses and return a '301 moved permanently' response with the new domain in the 'Location' field of the header. A 404 means 'not found' and in this case it's not strictly true.
Another option, similar to that proposed by #seth is to add the handler so it points to a static html page which you can use to explain to the user what happen, and present them with options.
You can include a meta redirect so that if they don't do anything after a few seconds they're automatically redirected.
Which option will work best is really up to you do decide.
You could set your 404 document to a CGI that redirects the user.
ErrorDocument 404 /cgi-bin/redirect-to-other.cgi