user1446650
user1446650

Reputation: 1255

Redirect people to website

I'm trying to figure out how I can redirect all traffic on a certain domain to my other website, based on the referrer.

For example, we only want people with no referrer, or linked to from our other website, to be able to access the site.

I was thinking of a script that would redirect everyone to a website of my choice, if their referrer didn't match the site I specify (but still allowed no referrer traffic).

Any help is appreciated!

Upvotes: 1

Views: 106

Answers (3)

Søren Løvborg
Søren Løvborg

Reputation: 8751

The following PHP code should do it (but remember, this cannot be used as a security measure, since the Referer header is provided by the client):

if (isset($_SERVER['HTTP_REFERER']) &&
    $_SERVER['HTTP_REFERER'] &&
    stripos($_SERVER['HTTP_REFERER'], "example.org") === false) {
    header("Location: http://example.com/", true, 302);
    exit();
}

Here we check that the client provided the Referer header, next that it is non-empty, and finally that it lacks the proper domain name (here "example.org"). If so, we redirect the client to another URL (here "http://example.com/") and exit.

Upvotes: 3

awenkhh
awenkhh

Reputation: 6111

you should configure that as RewriteRules in the webserver like Apache or nginx. Examples can be found easily in the documentation ... (I would give one, if I knew the used webserver software ...)

Upvotes: 1

Rich O'Kelly
Rich O'Kelly

Reputation: 41757

You can just check the HTTP referer (intentional misspelling) header:

if (!stristr($_SERVER['HTTP_REFERER'], "somesiteyouspecify") && !$_SERVER['HTTP_REFERER'] == '') ... redirect

Upvotes: 1

Related Questions