Redirecting URLs from specific referrals

They have: 14 posts

Joined: Jul 2001

Is there any way to redirect traffic from a specific referral URL to another page besides the original one it is linked to? I don't want to redirect all traffic, just traffic coming from one URL.

They have: 601 posts

Joined: Nov 2001

I *think* something like this should do it. Put the following in your .htaccess file within the directory you want this to happen.

RewriteEngine on
RewriteCond %{HTTP_REFERRER} !^.*domain.com.* [nocase]
RewriteRule ^.* <a href="http://www.domain.com/other.html" class="bb-url">http://www.domain.com/other.html</a> [R]
'
This is telling the server to redirect any requests from .domain.com to the new URL stated in the ReWrite rule. I think this code should work, although I didn't get time to test it.

You can create a .htaccess file by using anything that creates plain text file - usually windows notepad or similar. Place the above lines in it, and edit it to correspond to your domain and your redirected page then save the file as .htaccess - note the leading period is NOT a typo.

Upload this to the directoy (in ASCII transfer - this is only a plain text file remember) where you wish to enforce this rule.

Hope this helps.

- wil

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

You might have a problem in Windows with the filename. Windows doesn't like filenames that begin with a . and have no extension. So you might have to name it like so:
".htaccess", to insure success.

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

They have: 14 posts

Joined: Jul 2001

Hi. Thanks for the reply. I tried it, but it doesn't seem to work. I already had a .htaccess file on the server. I contacted my web host before I posted the initial post here, but they didn't know how to do it.

The browser window just sits there trying to retrieve the URL. The server is Unix. Does that matter? Do I put the www in front of the domain name? I tried it both ways and it didn't seem to matter. Same results. Any other suggestions? Your help is greatly appreciated.

Oh yea, is: [nocase] suppose to be included in the snippet of code? I left it there and was just wondering.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

scriptsolutions.com -- download domain director.

That may resolve your problem.

If you don't have mod_rewrite, then the .htaccess solution won't work.

Suzanne

They have: 14 posts

Joined: Jul 2001

Oh, maybe I didn't state this correctly; I'm sorry. The URL referral is coming from another web site, and I would like all traffic coming from that link redirected to a particular page on my web site rather than the one it is linked to. Is that possible?

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Yes, that's a totally different thing!

You can test for the referrer and then use cgi or php to redirect anyone coming from a particular domain.

So you'd have to figure this part out (sorry), but basically, if http_referer == "www.thisparticulardomain.com", then [insert redirect script here].

I think.

I'm going to move this over to server-side scripting, though, because it's definitely server-side.

Good luck!

Smiling Suzanne

They have: 601 posts

Joined: Nov 2001

Yes, that's exactly what that snippet I posted above should do. Well, not exactly. I've had a chance to play around with the syntax and found out that I was excluding everyone bar the given hostname, so I've removed the ! and it should work OK.

Use this modifed version:

RewriteEngine on
RewriteCond %{HTTP_REFERRER} ^.*<strong>domain.com</strong>.*$ [nocase]
RewriteRule ^.* <a href="http://www.domain.com/other.html" class="bb-url">http://www.domain.com/other.html</a> [R]
'
Obviously you would replace domain.com with the domain you are trying to redirect.

[nocase] simply means to ignore case. This means that the URL DoMaIn.com is treated exactly the same as domain.com.

You mentioned a problem with a htaccess file already being there? Ugh. You will most likely need to place the above in a htaccess file in the root directory of your site. It's usually quite safe to append this to an exisitng htaccess file however there might be conflicts.

What's in your .htaccess file now? Is this file in a specified directory or controlling your entire domain through being in the / root directory?

- wil

They have: 14 posts

Joined: Jul 2001

Hi Wil, thank you so much for helping. Suzanne too!

I tried the snippet just as you coded it replacing domain.com with theirsite.com and the other URL as http://www.mysite.com/page.html ... however, it still does not work.

There is nothing in the .htaccess file at the moment. There was a deny statement for a certain ISP, but that was a problem some time back which is now resolved, so I took it out.

The .htaccess file is located here: http://www.mysite.com/.htaccess

Is that what you mean by root directory or do you mean absolute path? Such as ...

/home/mysite/www/.htaccess

detox's picture

They have: 571 posts

Joined: Feb 2001

Seems sometimes like I am a dinosaur for primarily using ASP, however this is very easy and simple to do in ASP.

Do you want to try it with ASP? You shouldn;t even get to the page because the page will be parsed before it is displayed and the ASP check for the referring url will run and simply redirect your visitors...

They have: 601 posts

Joined: Nov 2001

OK.

Does this work?

RewriteEngine on
RewriteCond %{HTTP_REFERRER} ^<strong>http://www.domain.com/links.html</strong>$
RewriteRule ^.* <strong>http://www.domain.com/other.html</strong> [R]
'
Try and keep the tabs in there if you can. Enter the exact domain name and the the page where the link is coming from where I've marked as red. Enter the new URL to redirect yo where I've marked green.

Let me know how you get on.

- wil

They have: 14 posts

Joined: Jul 2001

Hi Wil, Sorry to say that snippet doesn't work either. I really feel fortunate that you folks are all trying to help. Thank you once again. I did everything exactly as shown. I am going to send this snippet to my domain host and ask them if there is another format that will work. Perhaps if they see it, maybe it will ring a few bells? Any other suggestions is GREATLY appreciated.

detox, I don't know anything about ASP. Doesn't that require an NT server? I would have no clue where to begin actually. But I wouldn't rule anything out as long as I don't need a different server than what I currently have.

detox's picture

They have: 571 posts

Joined: Feb 2001

My Bad..

sorry about that, should have paid more attention to the thread!. As far as PHP goes, I will leave it to the experts!

They have: 601 posts

Joined: Nov 2001

OK. Try this one! Smiling

RewriteEngine on
RewriteCond %{HTTP_REFERRER} ^<strong>http://www.domain.com/links.html</strong>$
RewriteRule ^<strong>/page/requested.html</strong>* <strong>/other.html</strong> [R]
'
Replace the following bits...

Replace the red text with the exact page the user is coming from.

Replace the blue text with the page he is trying to request on your server. Don't use the full domain, just /path/to/page.html will do.

Replace the green text with the page you want him redirected to. Again just use the partial path, i.e. /path/to/other.html.

How are you testing this, BTW? You are sure to be following the exact link from the other guy's page, right?

- wil

They have: 14 posts

Joined: Jul 2001

I am so sorry, but no that snippet isn't working either. Yes, I have made sure all URLs are active and correct. Maybe my web server just won't allow such things?

My web host was not much help. They told me they did not offer support for .htaccess and to study it through apache.org. At least they provided something, right?

I looked through apache.org late last night, but my brain wasn't focusing. (Long day at a clients' office. I dreamed of code last night. Argh.) I did see where the snippets you are giving me are derived, but with no previous knowledge of this type programming, there isn't any way I'd be able to figure it out without much in depth study.

The problem with this referring URL isn't too extensive yet unless this individual becomes even more weird. Smiling

Thanks so much for your help.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Would Apache give an error of some sort if the rewrite mod wasn't installed?

They have: 601 posts

Joined: Nov 2001

Yes, it would throw up a 500 Server Error on all pages under the directory where mod_rewrite is called.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.