Jump to content

redirect problem using HTTP_REFERER


mat1987

Recommended Posts

Hi,

 

I'm using this code to redirect visitors who are coming from another site (clicking on my ad on that site) to a page in my site where I can track them:

$referrer = $_SERVER['HTTP_REFERER'];
    if ( strstr($referrer, '://example.com/shop') )
       print ('<meta http-equiv="refresh" content="0; URL=http://www.mysite.com/1.php">');

The problem is that, by this code I can only redirect those who are coming from example.com/shop, but I need to redirect visitors coming from any sub directory or subdomain from example.com, not only a specific url in example.com

 

Is there any solution?

 

 

Thanks

Link to comment
Share on other sites

actually none. I'm not a pro :)

just need to detect the root domain and if the root domain is example.com, then do the redirect, regardless of any subdomains or subdirectories like blog.example.com or example.com/blog

Link to comment
Share on other sites

You can accomplish this by using a .htaccess file in the root directory and let apache handle it rather than php. I am not completely versed in .htaccess, but I have used it in the past to develop a redirect site similar to bit.ly. A Google search on .htaccess should get you in the right direction. I apologize I cannot give you concrete examples, but .htaccess came to mind when I read this post.

 

You can have the .htaccess file put the referrer information into url variables so they can be used in a $_GET method in your php function when the .htaccess file redirects.

 

Hope I can at least get you in the right direction. Good Luck!

 

Josh

Edited by joallen
Link to comment
Share on other sites

Thanks a lot guys.

The final working code is:

$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match('~example\.com$~', parse_url($referrer, PHP_URL_HOST))) {
       print ('<meta http-equiv="refresh" content="0; URL=http://www.mysite.com/1.php">');
}
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.