Pergunta

I'm trying to create a custom 404 page to include this information:

Sorry, this page was not found

The page you was trying to view was:

Please check the URL is spelt correctly, if it is then this page may have been moved, removed or never existed

I have tried using: $_SERVER['HTTP_REFERER'] to capture the URL that the user was trying to view before they got automatically redirected to the 404 page

working .htaccess code

Options -MultiViews
RewriteEngine On

ErrorDocument 500 /500.php
ErrorDocument 404 /404.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*)$ $1.php [L,QSA]

It's working now, thanks everyone who helped me

Foi útil?

Solução

You want to use $_SERVER['REQUEST_URI'] which gives you the URI which was given to access the current page. The result is in format /request_uri.php. Please refer to http://php.net/manual/en/reserved.variables.server.php for further details.

In your 404.php you could do the following:

The page you were trying to view was: <?php echo $_SERVER['REQUEST_URI']; ?>

Update:

Update your ErrorDocument declaration as follows:

# Remove http://www.example.com
ErrorDocument 404 /404.php 

From the docs (http://httpd.apache.org/docs/2.2/mod/core.html#errordocument)

Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server. This has several implications, the most important being that the client will not receive the original error status code, but instead will receive a redirect status code. This in turn can confuse web robots and other clients which try to determine if a URL is valid using the status code. In addition, if you use a remote URL in an ErrorDocument 401, the client will not know to prompt the user for a password since it will not receive the 401 status code. Therefore, if you use an ErrorDocument 401 directive then it must refer to a local document.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top