PHP custom 404 with url as argument??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rnoack
Forum Commoner
Posts: 34
Joined: Mon May 03, 2010 12:38 am

PHP custom 404 with url as argument??

Post by rnoack »

Hello!

I spent a little time thinking about this but not sure if its possible or how to approach.

I want to create a custom error document like 404.php which will appear when a user goes to a non existing page on my server, but I would like it if there was an argument ($_POST or $_GET.. or another) which contained the url which the user originally tried to access? Does anyone know if this is possible?

I looked on google but no luck really.
My webserver is apache if that will help.



After I have this I can easily program the rest but the reason/end result I am hoping to achieve is that I will be able to redirect all requests from http://<domain1>/<something>/ to http://<domain2>/<something>.php

note: i want to do it on 404 because if the <domain1>/something EXISTS, I do NOT want to redirect.

Anyway I don't need help with that part... just the first part.

Is there some way to make a custom 404 doc which can get the attempted url?

thanks!
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: PHP custom 404 with url as argument??

Post by hypedupdawg »

For a custom 404, all you need to do is create a text file in the root directory called ".htaccess" (just the file extension.) You then need to write:

Code: Select all

ErrorDocument 404 /404errordoc.php
or whatever your error page is.

I use a script on my server to find the url which I include on all my pages - you can find it below. The good thing about a 404 error doc is it does not actually change the url in the address bar - so if you find the actual url it is the same. So all I do is include a section like so:

Code: Select all

echo = "The requested file <b>\"" . getPageUrl('full') . "\"</b> was not found on this server.";
Get URL function:

Code: Select all

function getPageURL($type){
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on")
		{$pageURL .= "s";}
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80")
		{$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];}
	else
		{$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];}

	return $pageURL;
}
Post Reply