Ravand
Ravand

Reputation: 49

.htaccess redirect referrer on specific URL visit

I have understood the basics of redirecting a refferer through .htaccess but the only things i could manage to do with the .htaccess file was to either deny the entry of the whole folder or of a file but i never understood how to set up a redirection rule on a custom index page for example

What im trying to go for:

I want only 1 specific referrer to be able to visit this site "http://mydomain.com/index.php?page=Custom&pageID=7"

Everyone else that is not the specified referer should get redirected to another page.

I tried numerous php scripts for referrer detection but none of them seemed to work for me unfortunately

I would be very thankful for a solution :)

EDIT: Something like this ofcourse this doesn't work unfortunately

RewriteCond %{HTTP_REFERER} !^http://www\.refferersite\.org/ [NC]
RewriteRule ^/index.php?page=Custom&pageID=7 http://newsite.com/index.php [L]"

Upvotes: 1

Views: 9091

Answers (2)

user51810
user51810

Reputation: 3

can u try this?:

RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !yourdomain\.com [NC]
RewriteCond %{HTTP_REFERER} !alloweddomain\.com [NC]
RewriteRule .? - [F]

The first RewriteCond checks that the referrer is not empty. The second checks that it doesn't contain the string yourdomain.com, and the third that it doesn't contain the string alloweddomain.com. If all of these checks pass, the RewriteRule triggers and denies the request.

Deny referrals from all domains except one

Upvotes: 0

Daniel Gutierrez
Daniel Gutierrez

Reputation: 21

I'm going to give a simple example, things become a little more complicated if you're taking in form variables or something. But for your .htaccess you would want something like this:

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} referringsite\.com [NC,OR]
RewriteRule .* index.php?page=Custom&pageID=7 [L,QSA]

The rewrite condition must be true in order to trigger the rewrite. At which point the .* tells it to rewrite the entire query to index.php?page=....

The L will make it stop processing other rules (so if you have any, they don't rewrite this rewrite).

The qsa will maintain any query parameters. So if they are also passing in something like ?campaign=my_ad_campaign, it gets attached to the end of your index page.

Hope this helps!

--Update-- I think you want this

RewriteCond %{HTTP_REFERER} !referringsite\.com [NC,OR]
RewriteRule index.php?page=Custom&pageID=7 index.php [L]

Do not include the http in the referring site.

Upvotes: 2

Related Questions