mat1987 Posted May 22, 2014 Share Posted May 22, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/288672-redirect-problem-using-http_referer/ Share on other sites More sharing options...
requinix Posted May 22, 2014 Share Posted May 22, 2014 You bet there is. Many, in fact. What have you tried yourself? Quote Link to comment https://forums.phpfreaks.com/topic/288672-redirect-problem-using-http_referer/#findComment-1480421 Share on other sites More sharing options...
mat1987 Posted May 22, 2014 Author Share Posted May 22, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/288672-redirect-problem-using-http_referer/#findComment-1480422 Share on other sites More sharing options...
cyberRobot Posted May 22, 2014 Share Posted May 22, 2014 Have you tried just searching for "example.com"? Quote Link to comment https://forums.phpfreaks.com/topic/288672-redirect-problem-using-http_referer/#findComment-1480430 Share on other sites More sharing options...
cyberRobot Posted May 22, 2014 Share Posted May 22, 2014 You may also want to check out PHP's parse_url() function: http://www.php.net/manual/en/function.parse-url.php Quote Link to comment https://forums.phpfreaks.com/topic/288672-redirect-problem-using-http_referer/#findComment-1480432 Share on other sites More sharing options...
joallen Posted May 22, 2014 Share Posted May 22, 2014 (edited) 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 May 22, 2014 by joallen Quote Link to comment https://forums.phpfreaks.com/topic/288672-redirect-problem-using-http_referer/#findComment-1480489 Share on other sites More sharing options...
mat1987 Posted May 23, 2014 Author Share Posted May 23, 2014 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">'); } Quote Link to comment https://forums.phpfreaks.com/topic/288672-redirect-problem-using-http_referer/#findComment-1480564 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.