user3123746
user3123746

Reputation: 25

Capturing and logging broken links on a custom 404 page

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.

Upvotes: 1

Views: 819

Answers (1)

anubhava
anubhava

Reputation: 785098

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"]

Upvotes: 1

Related Questions