How to Rewrite Rule if Link is coming from other site

I am stuck on a point. I have a site with affiliate links, prob is google caching the affiliate links also. I want if some one is clicking that affiliate link from google ie from some other site than the one specified for then it should be redirected to my main page.

Like: main link : [noparse][/noparse]

Affiliate Link: [noparse][/noparse]

If affiliate link is clicked from google it should be redirected main link

I will be greatful for your help.


This is not something you should let Apache handle.
Although Apache is perfectly capable of handling it, if you let Apache rewrite it you application will never know the affiliate part was in the URL, which defeats the purpose of having affiliate links in the first place.
If you want to get rid of the affiliate part, let the application capture it first and let it store in a cookie or session, and then 301 redirect to the current URL without the affiliate part.
Does that make sense?