Hello,

I'm using Apache web server and PHP. I, already, succeded to make a custom 404 document. It is found in /messages/e404.php. So, right now, any URL typed that request a page does not exist on the web server, will be redirected to the regarded document above.

Suppose the following, I requested the a page such as http://localhost/some/awer.hdx which is not found on the server, so, the server will redirect the browser to the custom error document.

What I need to know is how to make the requested URL of the not found page, http://localhost/some/awer.hdx to be displayed on the custom error message, inorder to make broken link tell to the webmaster.

    If you're using Apache's ErrorDocument 404 directive, then try echo'ing out either $SERVER['REQUEST_URI'] or $SERVER['REDIRECT_URL']. For example, if I try to go to http://localhost/sddff/fred.php?hi=there on my server, those two variables will be set to:

    [REDIRECT_URL] => /sddff/fred.php
    [REQUEST_URI] => /sddff/fred.php?hi=there

    To get the full address, as you showed, you could do this:

    echo '404 error: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

      Well, if you use .htaccess, you can do this:

      ErrorDocument 404 "Error 404 code goes directly in here, no line breaks.

      Notice that there are no line breaks. This has to be or your .htaccess will get confused and fry your site. You also leave out the last double quote, not sure why, but that's how it works. Ask anybody. 😉

        The point was for him to be able to show the incorrect URL displayed. He's already redirecting 404's to a PHP script... he just needed the bit of PHP to retrieve the incorrect URL.

        Also, you don't need an ending quote because it's not actually a delimited string - when Apache sees the ", it reads all successive data as a string parameter until it either reads a line break or the EOF is reached.

          I don't think you understand. This doesn't work as a redirect. The URL they requested stays in the browser's URL bar. Also, you can use getenv("HTTP_REQUEST") to see what page they tried to get to.

            Technically it's not a redirect, no, the webserver just calls the 404 file when it fails to find the requested file.

              No, I've used both forms of 404 catching by .htaccess. The one I showed just displays the code. The other way actually sends you to another page, changing the URL in the browser. Is he using something other than .htaccess to catch 404 errors?

                Sends you to another page? What did you do? The method for creating a 404 page is usually done like so:

                ErrorDocument 404 /404_page.php

                If you added the full path, i.e. http://mydomain.com/404_page.php then yes, Apache will see that as a redirect instead of an internal redirect.

                  Oh, ok. But couldn't you still use getenv("HTTP_REQUEST") to see what page they tried to find?

                    I don't see any server variable called HTTP_REQUEST... only one called REQUEST_URI.

                      I could have sworn there was one... nevermind. I drained my mind earlier today, there's none left. Please continue your conversation without my interruptions.

                        Hi,
                        Thanks for your valuable help. I was informed very well due to your replies.
                        I found another approach to get the URL of the missing page. It is the client side approach. i.e JavaScript.
                        Using [FONT=Lucida Console]document.location[/FONT], will give the location of the page, since, apache does not lead to change the missing page URL from the browser's address bar.
                        A text file is attached named e404.txt show what I have done. To test it, please change its extension to .php

                          And what happens when the person has disabled Javascript? Or their browser doesn't like your JS coding? I tested this in Opera and FireFox, and saw this:

                          URL requested is: 

                          So, your code only works for IE users who have enabled Javascript.

                            You are right. JavaScript is not the most effective solution in this case.

                              I use PHP whenever possible. Javascript is only good when you have to do something after the page is loaded.

                                Write a Reply...