Forum Moderators: phranque

Message Too Old, No Replies

Making a 404 Print the Requested URL

The REQUEST_URI is redundantly redundant

         

mrfusion

4:57 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



I'd like to think I'm not a newbie when it comes to stuff like this, but I have very limited experience with all this Apache stuff. That said, I'm trying to figure out whether or not I can get my server to display a 404 page without changing the mistyped URL.

Currently whenever a user types in the wrong URL, the address automatically redirects / changes to the location of the 404 page (i.e. example.com/blah -> example.com/errors/404.php). I'd like for the URL to remain the same while the site loads up the 404 page, but I have yet to figure that out. I figured I could use the REQUEST_URI command in the script:

<html> 
<body><h1>404 File Not Found</h1>
<hr>
<p><i><?php
echo $_SERVER['REQUEST_URI']
?></i></p>
</body></html>

However, I soon realized that since the URL automatically changes, this seemed a lost cause:

404 File Not Found
_________________________________

/errors/404.php

After Googling for a while, I came across a couple of .htaccess tricks [corz.org] and tips [garnetchaney.com] that never quite panned out the way I hoped. I tried tons of permutations with no success; most of the time I got this stupid message: "Redirection limit for this URL exceeded. Unable to load the requested page."

Here's an example of my current .htaccess file:

ErrorDocument 403 http://example.com/errors/403.php 
ErrorDocument 404 http://example.com/errors/404.php

RewriteEngine On 
Options Indexes Includes +FollowSymLinks ExecCgi

# The following commented lines would not behave itself. 
# (It was later punished and sent to bed without supper.)

# RewriteBase / 
# RewriteCond %{REQUEST_FILENAME}!-f
# RewriteCond %{REQUEST_FILENAME}!-d
# RewriteRule ^(.*)$ /$1 [R=301,L]

AddType text/vnd.wap.wml wml 
AddType image/vnd.wap.wbmp wbmp
AddType text/vnd.wap.wmlscript wmls
AddType application/vnd.wap.wmlc wmlc
AddType application/vnd.wap.wmlscriptc wmlsc
AddHandler server-parsed wml txt
AddType application/x-httpd-php .php

DirectoryIndex index.php index.shtml index.html index.htm index.cgi index.wml

Am I missing something? Any pointers? Anyone? Thanks in advance!

-Danny

jdMorgan

7:39 pm on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> missing something?

Yes, sorry, reading the documentation...

The ErrorDocument [httpd.apache.org] description clearly states that you must use a local URL-path and not a canonical URL if you wish the server to return the correct server response code. Otherwise, it will return a 302 redirect response code.


ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php

Using ErrorDocument to redirect to another domain has its uses, but it causes problems just like this one.

Jim

mrfusion

12:22 am on Jul 7, 2005 (gmt 0)

10+ Year Member



Yep, I'm just another stupid newbie. It worked perfectly. Thanks for your help and for not telling me blatantly to go RTFM. (I will though, I promise.) :)

-Danny