View Full Version : Redirect based on HTTP_REFERER

06-21-2004, 07:52 AM
Anyone know why this won't redirect, it just sits there and sits there and sits there. If I try to echo something out, or do pretty much anything else aside from header(), it works.


06-21-2004, 08:04 AM
The manual for header has a space, and that's how I do it on all my sites.. Like this:

header('Location: http://www.example.com/');

Note the space between Location: and http://

Maybe that's it?

06-21-2004, 08:09 AM
I've never noticed a difference with or without the space, or really paid attention.

Either way, that wasn't it. :)

06-21-2004, 08:15 AM
Do you have any code before this happens that prints to the screen? Nothing can be echoed to the browser before the header code can be used.

Create a blank php page with *just* the header code in it and see if that works.

06-21-2004, 08:34 AM
This is the first code that's output, above my DOCTYPE and everything else. I'm redirecting an existing link to my site, so there's no way to check if the code works other than to check the particular page it's linking to.

The overall code works. I can echo out the referring link. I can kill it, die(), and do anything else with it. But the redirect doesn't work. I've even cleared my .htaccess to make sure there wasn't any confusion with my rewrites/redirects in that code. But I get the same result.

06-21-2004, 08:38 AM
Testing a link: http://www.lit.org/shawn

Shawn, The above link tests your code you posted EXACTLY as you posted it. I have "somesite" set to websitepublisher.net It works on my server.

06-21-2004, 08:44 AM
Weird. Ok go here and click the link to my site, in the post by Mark.


I have it set to "htmlforums". For me, it just sits there and sits there.

06-21-2004, 08:50 AM
That is odd, I'm trying it from that link too and it's just sitting there. Can you post the entire index file from that directory?

06-21-2004, 08:57 AM
Are you printing out anything on the page? If you send the browser a redirect and then print something out it might get confused. You shouldn't have any echos or prints in that file.

06-21-2004, 09:07 AM
Hmm, weird thing I just noticed. If I hit the Stop button on my browser, it redirects. But the page stops loading of course.

My site is broken into multiple modules, so there's no way to paste in a full page without opening several different files. That whole page is just (X)HTML. There's a header file that's used throughout every page (where I'm placing the code). It's basic header stuff (DOCTYPE, head tags, meta tags, stylesheet code, title tags, etc).

My site is nothing but includes and a simple if/else statement that changes the title tag based on the page. No echo/prints.

Ah well, I'll figure it out. It's something with that header() function because even a typical meta refresh works. But that still lands on the page, then redirects.

06-21-2004, 12:50 PM
I got it straightened out by just using the htaccess file. Although I still played hell with that for about 30 minutes before I finally ran across a page that explained something no other site had. The way I was trying to go about it, the server kept trying to reexecute the htaccess file, putting it in an infinite loop. I think it's the same thing that was causing the problem earlier even with the PHP redirect.

I was able to redirect to other sites, but not my front page.

But a simple check that the requested uri was /flash/, everything works as it should. :)

RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain.com/.*$
RewriteCond %{REQUEST_URI} ^/flash/
RewriteRule \.*$ http://visualintensity.com [L,R]

06-21-2004, 01:15 PM
I assume you weren't putting the php file in /flash then? If you had put the .htaccess file in the /flash directory no loop should have happened even without the uri check. Same things goes for the php. I had a feeling it was in a loop. That's what it felt like.

06-21-2004, 01:38 PM
Well, I put the PHP in my header.php file. Every page on the site shares that file. My htaccess file in root.

06-21-2004, 02:48 PM
Well, I put the PHP in my header.php file. Every page on the site shares that file. My htaccess file in root.

That's probably why. It was trying to redirect to your main page and the referer was the same so it looped.

I usually put my redirects in the folder they are redirecting beause I'm prone to this kind of looping too ;)