404 error - Undefined index: REDIRECT_URL

6.1k views Asked by At

I don't understand. I set the 404 error in my .htaccess and it is redirected to error.php when I type something like http://{localhost}/mywebiste/xxx/xxx/xxx/xxx/

But I get this error,

Notice: Undefined index: REDIRECT_URL in C:\... 
Notice: Undefined index: REDIRECT_STATUS in C:\...

for,

$_SERVER["REDIRECT_URL"];
$_SERVER['REDIRECT_STATUS'];

.htaccess,

RewriteEngine on
ErrorDocument 404 http://{localhost}/projects/.../error.php

error.php,

$redirect_url = $_SERVER["REDIRECT_URL"];
$status = $_SERVER['REDIRECT_STATUS'];

Any ideas?

I have tried the codes from these two sites and none of them work,

1 2

Also, I'm on EasyPHP Devsever. I want to check whether there are

print_r($_SERVER);

I get this,

Array
(
    [TMP] => C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/binaries/tmp
    [HTTP_HOST] => localhost
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_ACCEPT_LANGUAGE] => en-GB,en;q=0.5
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_COOKIE] => PHPSESSID=204j1jvbinr4j1i0ebg9eq0ue5
    [HTTP_CONNECTION] => keep-alive
    [PATH] => C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\Common Files\Roxio Shared\10.0\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.8.0_05\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Git\cmd;C:\Users\lau_thiamkok\AppData\Roaming\npm;C:\Program Files\nodejs;C:\Program Files (x86)\Git;C:\PROGRA~2\EASYPH~1.1VC\Apache\bin;C:\PROGRA~2\EASYPH~1.1VC\PHP
    [SystemRoot] => C:\Windows
    [COMSPEC] => C:\Windows\system32\cmd.exe
    [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    [WINDIR] => C:\Windows
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache/2.4.7 (Win32) PHP/5.5.8
    [SERVER_NAME] => localhost
    [SERVER_ADDR] => 127.0.0.1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 127.0.0.1
    [DOCUMENT_ROOT] => C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb
    [SERVER_ADMIN] => [email protected]
    [SCRIPT_FILENAME] => C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb/projects/tests/php-boilerplate/404/basic/error.php
    [REMOTE_PORT] => 55762
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => code=404
    [REQUEST_URI] => /projects/tests/php-boilerplate/404/basic/error.php?code=404
    [SCRIPT_NAME] => /projects/tests/php-boilerplate/404/basic/error.php
    [PHP_SELF] => /projects/tests/php-boilerplate/404/basic/error.php
    [REQUEST_TIME_FLOAT] => 1416567060.231
    [REQUEST_TIME] => 1416567060
)

But I don't see REDIRECT_* as in the list here.

Any ideas how I can get REDIRECT_* working in my localhost?

1

There are 1 answers

5
anubhava On BEST ANSWER

You can avoid using full http:// in ErrorDocument 404 directive to get both values populated like this:

ErrorDocument 404 /projects/full/path/to/error.php

With this you will see both these values populated:

$_SERVER["REDIRECT_URL"]; // /mywebiste/xxx/xxx/xxx/xxx/
$_SERVER['REDIRECT_STATUS']; // 404