How to redirect user coming from certain site
I'm not expert and looked all over the internet for a meta-refresh code or javascript does not matter. I want to redir开发者_StackOverflow社区ect for example:
Site1 redirect to Site2 ,but only when someone comes from url=youtube.com for example, if they don't come from youtube.com they should stay in Site1.
maybe something like that
<SCRIPT LANGUAGE="JavaScript">
if(user come from = "youtube.com"){
window.location = 'http://Site2.com';
} else {
window.location = 'http://Site1.com';
}
</SCRIPT>
If you are running PHP on the server, I'd most definitely do it server-side instead of with Javascript.
Add the following above anything else in your index.php file of site1.com. (If you have an index.html file, simply rename it to index.php.)
<?php
//add any referrers that should be redirected to the array
$refs = array('http://youtube.com','http://another-domain.com');
foreach($refs as $r) {
if(preg_match('/'.$r.'/', $_SERVER['HTTP_REFERER']))
header('Location: http://site2.com/');
}
?>
(edit: added PHP tags and explanation, improved code)
document.referrern will do the job : https://developer.mozilla.org/en/document.referrer
so in your example it would be (it's a short version of if/esle):
document.referrer=="www.youtube.com"?window.location = 'http://Site2.com':window.location = 'http://Site1.com'
or if you wanna write it completely, it would be (notice the "==" to compare the values):
if(document.referrer=="www.youtube.com"){
window.location = 'http://Site2.com'
}else{
window.location = 'http://Site1.com'
}
You would need to use document.referrer to get the location where the user came from. It is not 100% reliable.
var ref = document.referrer;
if(ref && ref.toLowerCase().indexOf("youtube.com")!==-1){
window.location.href="site2.html";
}
精彩评论