简体   繁体   中英

Redirect based on REFERER from different pages of the same domain

I'm looking for a .htaccess solution based on a REFERER from a specific page in the same domain:

Example #1

If a user clicks from the facebook homepage like:

https://www.facebook.com/

Redirect to:

https://www.mywebsite.com/targetpage

Example #2

If a user clicks from another page on Facebook, like:

https://www.facebook.com/ads/library/?somequery....

Redirect to:

https://www.mywebsite.com/

Try the following using mod_rewrite near the top of your .htaccess file:

RewriteEngine On

# Link from FB homepage
RewriteCond %{HTTP_REFERER} ^https://www\.facebook\.com/$
RewriteRule !^targetpage$ /targetpage [R=302,L]

# Link from another FB page
RewriteCond %{HTTP_REFERER} ^https://www\.facebook\.com/ads/library?somequery$
RewriteRule !^$ / [R=302,L]

You need to check that we are not already on the targetpage in order to avoid a redirect loop.

HOWEVER, (usual disclaimer about the HTTP Referer)... The HTTP Referer is unreliable at best. Particularly in modern browsers with the referrer-policy, since the referring website (eg. Facebook in this example) can control exactly what Referer header the browser is permitted to send.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM