homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirect links coming from a specific domain
Using .htaccess to redirect links to a different page on my site

 4:24 am on Jun 8, 2007 (gmt 0)

I've spent the past week researching a solution to this problem, and have utilized many of the great resources available on Webmaster World. However, what I want to happen is still not working. So, I'm posting what I have in hopes someone can take a quick look at it, and offer some help.

Someone is on www.xyz.com looking at some page, and they click on a link URL=http://www.mysite.com I want that person to be redirected to www.mysite.com/dt/index.html I only have control over mysite.com, not xyz.com

Code in .htaccess file:
RewriteEngine on
RewriteCond {%HTTP_REFERER} ^(www\.)?xyz.com$
RewriteRule ^(.*)$ http://example.com/dt/$1 [R,L]

This code has been through several changes, but this is the latest incarnation, and it's still not working. Links from xyz.com still go to mysite.com/index.html If this can be accomplished another way, I'm perfectly ok with it. I'm not stuck on .htaccess for any reason other than I was told it was the only way. My hosting provider is 1and1, and they told me .htaccess use is permitted. Thank you so much for any help you can offer.

[edited by: jdMorgan at 5:24 am (utc) on June 8, 2007]
[edit reason] example.com [/edit]



 5:24 am on Jun 8, 2007 (gmt 0)

Your code would only work if the referrer was exactly "www.xyz.com" or "xyz.com".

But it won't be; It will have "http://" at the beginning, and in most cases, at least a slash at the end.

I'd recommend changing your RewriteCond to:

RewriteCond %{HTTP_REFERER} ^https?://(www\.)?xyz\.com

Note that I removed the "$" pattern end-anchor, so any referring page from that site will match.

This condition will now match if the referrer is from any page on www.xyz.com or xyz.com, whether it's a secure (https) or insecure (http) page.



 6:18 am on Jun 8, 2007 (gmt 0)

Thanks jd, for the quick response. I tried it, but links from xyz.com are still landing in mysite.com/index.html Most of the links are actually coming from "http://subdomain.xyz.com/blahblah" if that makes a difference.

Current code looks like:
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?xyz\.com
RewriteRule ^(.*)$ http://example.com/dt/$1 [R,L]

Any ideas? Thanks again!

[edited by: Raid at 6:20 am (utc) on June 8, 2007]


 6:32 am on Jun 8, 2007 (gmt 0)

> "http://subdomain.xyz.com/blahblah" if that makes a difference.

Yes, it makes a difference -- The code has to match all the observed referrer variants.

So try:

RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)*xyz\.com

That will match the domain xyz.com, or any subdomain, sub-subdomain, sub-sub-subdomain, etc.

The pattern reads, "match http or https, followed by a colon and two slashes, followed by any number (including zero) of the sequence: (one or more characters not a period, followed by a period) followed by xyz.com, followed by anything or nothing."

Remember to completely flush your browser cache after any change to the code, or after successfully loading any of the normal pages -- If the normal page is in your browser cache, then your browser will not send a request to the server, it will serve its cached copy. If a request isn't sent to your server, then code on your server cannot affect anything, regardless of the referrer.



 12:13 am on Jun 9, 2007 (gmt 0)

Excellent! It's redirecting now. Thank you so much -- I asked that same question on another "forum", and was told it made no difference. Thank you Jim.

There seems to be a quirk in the RewriteRule however. Links are being redirected successfuly, but they are landing on http://example.com/dt/dt/dt/dt/dt/dt... Firefox is reporting an error msg: "The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Current code:
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)*xyz\.com
RewriteRule ^(.*)$ http://example.com/dt/$1 [R,L]

I have tried a few modifications with no success. Can you see anything in my code that would cause this?


 12:24 am on Jun 9, 2007 (gmt 0)

Yes, you've got a redirection loop going there. So add a RewriteCond to prevent the rule from being applied once you've already redirected the client to the /dt/ subdirectory:

RewriteEngine on
RewriteCond $1 !^dt/
RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)*xyz\.com
RewriteRule (.*) http://example.com/dt/$1 [R=302,L]



 1:18 am on Jun 9, 2007 (gmt 0)

Jim, thank you again. It works brilliantly. What a learning experience this has been. I've never had so much trouble with four lines of code. Patience is a true asset when coding .htaccess.

Your explanation helped me a great deal with the HTTP_REFERER line. Just to make sure I understand the last change: RewriteCond $1!^dt/ is basically saying, "if you're not being refered from the dt/ directory ...then continue to the next line of code" Is that accurate?

Thanks once again. I think I may have found a new home on these forums.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved