Jump to content

.htaccess - 404 redirection - HTTP_REFERER


PHP_tyro

Recommended Posts

 

Hi folks.

 

My server and web programming expertise is with Windows servers and ASP. My PHP is rudimentary at best, and I'm trying to learn as I go along while solving practical problems.

 

What I need to do is redirect to a specified page on on a different domain when an invalid URL is entered into the browser.

 

For example if:

http://www.MyActualDomain.com/BadFolder/index.php

does not exist, then the visitor is redirected to:

http://www.MyOtherDomain.com/404handler.php

 

As I understand it the conventional way to do that is via a redirect in the .htaccess file.

 

I have done that and it worked fine.

 

NOW FOR THE PROBLEM:

 

A script in 404handler.php needs to know the bad URL that caused the 404.

 

He is a snippet of test code I put together trying to figure out these environment variables:

 

<?

$host = getenv("REMOTE_HOST");

$referrer = getenv("HTTP_REFERER");

$path = getenv("REQUEST_URI");

 

echo "REMOTE_HOST  = " . $host . "<br />";

echo "HTTP_REFERER = " . $referrer . "<br />";

echo "REQUEST_URI  = " . $path . "<br />";

?>

 

REMOTE_HOST returns my own IP as expected, but the other two strings are empty.

 

I've also tried $_SERVER['REQUEST_URI'] in place of the getenv method.

 

Anyone got a solution?

 

if it matters, here's PHPinfo on the server where 404handler.php resides:

 

PHP Version 5.0.5

 

System Windows NT NT24 5.2 build 3790

Build Date Sep 5 2005 15:50:13

Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--with-gd=shared"

Server API CGI/FastCGI

Virtual Directory Support enabled

Configuration File (php.ini) Path C:\WINDOWS\php.ini

PHP API 20031224

PHP Extension 20041030

Zend Extension 220040412

Debug Build no

Thread Safety enabled

Zend Memory Manager enabled

IPv6 Support enabled

Registered PHP Streams php, file, http, ftp, compress.zlib

Registered Stream Socket Transports tcp, udp

 

 

Link to comment
Share on other sites

I haven't done this for a while but I think when you type the address into the browser address bar or use an absolute link it is called an "external" redirect and you won't get HTTP_REFERER etc.  What I did last time was like

[...]
if (!isset($_SERVER['HTTP_REFERER'])){
//user has mistyped url
}elseif (isset($_SERVER['HTTP_REFERER'])){
//your fault.  Internal error so HTTP_REFERER & REQUEST are now usable.
}

 

Link to comment
Share on other sites

Unfortunately the $_SERVER information provided by windows is different to that in a linux environment, thus I recommend you do the following to find what variables you CAN get:

echo "<pre>";
print_r($_SERVER);
echo "</pre>";

 

(oh and use $_SERVER NOT getenv(), they ARE different )

Link to comment
Share on other sites

Thanks for the response guys.

 

Unfortunately the $_SERVER information provided by windows is different to that in a linux environment

 

Well, that was illuminating aschk, and I think it's given me the answer. I used your code snippet to see what information was revealed, and I came up with the following...

 

If I browse to http://www.MyActualDomain.com/BadFolder and the 404handler.php is located on a Windows server, it doesn't appear as if I can get the name of /BadFolder at all.

 

If I browse to the same nonexistent location and the 404handler.php is located on a Linux server, I can get /BadFolder from two variables:

 

[REDIRECT_URL] => /BadFolder

 

[REQUEST_URI] => /BadFolder

 

The difference between those two, I'm not sure. Any enlightenment?

 

Anyway, I can live with having 404handler.php on a Linux server.

 

 

However I've just discovered another problem with this, which I'll post in a separate message.

 

Link to comment
Share on other sites

On the Linux server, the first line of .htaccess reads: ErrorDocument 404 /404handler.php

 

That works fine for the example in the message above, where I was entering:

http://www.MyActualDomain.com/BadFolder

into the browser.

 

However, if I browse to:

http://www.MyActualDomain.com/BadFolder/DoesNotExist.html

then the browser opens at the home page of:

http://www.MyActualDomain.com

 

I haven't put any redirection in 404handler.php yet, so it's not as if I'm being sent to 404handler.php then back to the home page. It's as if the ErrorDocument line in .htaccess is being ignored if a full URL including pagename.ext is entered into the browser.

 

Any suggestions?

 

Link to comment
Share on other sites

 

OK, after a bit more experimentation I can provide more accurate information than in the previous message.

 

So as I said...

 

I'm setting up a 404 redirect on a Linux server and have this line in .htaccess:

ErrorDocument 404 /404handler.php

 

That works fine EXCEPT when the nonexistent page that's called for has the extension .html -AND- there is subfolder involved.

 

In other words, the redirect works for all these examples:

 

http://www.MyDomain.com/DoesNotExist.htm

http://www.MyDomain.com/DoesNotExist.php

http://www.MyDomain.com/DoesNotExist.asp

http://www.MyDomain.com/DoesNotExist.html

http://www.MyDomain.com/BadFolder

http://www.MyDomain.com/BadFolder/DoesNotExist.htm

http://www.MyDomain.com/BadFolder/DoesNotExist.php

http://www.MyDomain.com/BadFolder/DoesNotExist.asp

 

But in this case:

 

http://www.MyDomain.com/BadFolder/DoesNotExist.html

 

There is no redirection and the browser just opens at the home page of http://www.MyDomain.com.

 

I guess I must be missing something.

 

Please tell me what it is.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.