Forum Moderators: phranque

Message Too Old, No Replies

Redirect Based On Referrer

         

static

10:07 am on Feb 3, 2009 (gmt 0)

10+ Year Member



I've read several redirect methods for .htaccess that I read on this forum(most from JD)and can't seem to get them to work. So possibly it has to do with the referrer url I'm using.

What I'm trying to do is redirect from yahoo.answers . I post links on there to my websites from time to time and link to websites of mine. An example of what I'm trying to redirect would be

[answers.yahoo.com...]

In the past, I would just setup a landing page on the index page of my domain. I now want to use this domain as an actual website. So I would like to redirect the above referrer url to [mywebsite.com...] to be able to still use my old landing page instead of landing on my new index page.

Now as you can see I'm dealing with a sub-domain here with answers.yahoo.com

I did try putting an extra forward slash in the mod rewrite answers/.yahoo/.com but that didn't work. I'm sure I'm not doing something correctly.

Whats the correct way to redirect via .htaccess with the above url I mentioned from answers.yahoo?

g1smd

2:05 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What code have you already tried?

static

3:33 pm on Feb 3, 2009 (gmt 0)

10+ Year Member



Hello, I don't know which ones specifically off the top of my head but I've tried several listed in this forum. I'm pretty sure I'm not writing it correctly.

If you were me, what would you put in .htaccess to get the above yahoo.answers referral url to redirect ?

jdMorgan

3:59 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can save everyone a lot of time if you will post the last one you tried, and tell us how you tested it, what results you got, and how those results differed from your expectations.

That way, our members know where you started, and don't have to 'start all over' and explain everything (this latter being impossible, really). Please consider that the time our members donate answering questions here is valuable; They will be glad to help you get your code working, but this is your project.

Our Forum Charter [webmasterworld.com] explains our policies and describes how to get the most from this forum.

Thanks,
Jim

static

6:54 pm on Feb 3, 2009 (gmt 0)

10+ Year Member



This is what I tried using

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(cgi\.)?ebay\.com [NC]
RewriteRule \.html?$ /newpage.htm [NC,L]

I found this posted by JD from another thread. I've tried several others similar to the one above that also don't work.

What I did was replaced ebay with yahoo\.com and then tried answers.yahoo\.com and answers\.yahoo\.com. I also tried the full url I mentioned in the 1st post.

The link from yahoo answers when clicked on still just goes to my homepage so I know its not working.

jdMorgan

7:43 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, that focuses things a bit more narrowly.

The correct referrer pattern is
^http://answers\.yahoo\.com [NC]
therefore, that should have worked. But...

  • Did you completely flush your browser cache before testing, and between tests of different referrers? (If not, your browser will very likely use the cached page and server response, leading to invalid test results.)

  • Do you have any other working mod_rewrite code in this .htaccess file? If not, did you get any error info logged in your server error log? (Some servers require additional configuration settings before you can use mod_rewrite, and if so, will throw an error if you try to use mod_rewrite without those settings.)

  • Be aware that the referrer will be preserved and carried forward to the (internal) request for the new path; You will likely need to exclude "/newpage.html" itself from being re-written in order to avoid an "infinite" rewrite loop.

    Jim

  • g1smd

    7:45 pm on Feb 3, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    The fourth word of your initial post is "redirect", but the code in your last post is for a "rewrite".

    So, do you want the visitor to see a different URL appear in their browser address bar to the one they requested?

    If you simply replaced "ebay" with "yahoo", then it could never match "answers.yahoo.com" because the code above has provision for an optional "cgi" subdomain, and has no provision for any other subdomain names.

    static

    8:50 pm on Feb 3, 2009 (gmt 0)

    10+ Year Member



    JD, thanks for clarifying, but what you said to do I've already tried and it doesn't change anything or redirect to my new page.

    I have cleared my cache, cookies, browsing history etc and tried on several browsers.

    I do have mod rewrites for other sites of mine on the same server but they are not trying to redirect by referrer but rather old page to new page.

    I have nothing else in my .htaccess file other than

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} ^http://answers\.yahoo\.com [NC]
    RewriteRule \.html?$ /newpage.htm [NC,L]

    Do I need something else in there possibly?

    g1smd

    8:57 pm on Feb 3, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    It cannot ever "redirect". That code is for a "internal rewrite". That is a very important difference.

    static

    9:01 pm on Feb 3, 2009 (gmt 0)

    10+ Year Member



    g1smd. Maybe I'm using wrong terminology then..

    Do you know the correct .htaccess code that will take a referring URL and send the visitors from that URL to a different page on my site?

    g1smd

    9:07 pm on Feb 3, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    The question I asked above, and which it is vital to know the answer to, was:

    So, do you want the visitor to see a different URL appear in their browser address bar to the one they requested?

    static

    9:11 pm on Feb 3, 2009 (gmt 0)

    10+ Year Member



    no just a different page. Right now its going to [mydomain.com...]

    I want it to go to [mydomain.com...]

    This way I can now use my homepage(index) as an actual website for something else and still utilize yahoo answers traffic if I can send the visitors to a new page on my site.

    g1smd

    10:54 pm on Feb 3, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Yes, you want it to "go" there, and you want to get the content from there, but again, the terminology you are using is unclear.

    Do you want a rewrite (where the URL in the browser doesn't change from what the user originally requested) or do you want a redirect (where the user sees the URL in the browser address bar updated to show the new location that is delivering the content)?

    jdMorgan

    11:54 pm on Feb 3, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    It makes no difference to the problem being discussed whether an internal rewrite or an external redirect is used. The problem is that the code (currently an internal rewrite) is not working at all. It would therefore be appropriate to defer the discussion of whether a rewrite or a redirect might be "better" until *either one* can be made to work.

    Static,
    Do you have any indication of an error? Is there anything in your server error log?

    As I noted, you should actually be getting an error with that code, because it invokes an infinite loop.

    Addressing that problem and one other possible problem, I'd suggest:


    Options +FollowSymLinks
    RewriteEngine on
    #
    RewriteCond %{HTTP_REFERER} ^http://answers\.yahoo\.com [NC]
    RewriteCond %{REQUEST_URI} !^/newpage\.htm$
    RewriteRule \.html?$ /newpage.htm [NC,L]

    Again, completely-flush your browser cache after uploading this, and between each "test" of yahoo-answers and non-yahoo-answers referrers. Otherwise, the browser's caching function will invalidate your test results. (You may just want to temporarily disable your cache by setting it's size or cache-persistence time to zero. If you do this, be sure to re-enable it when you're finished testing!)

    Also, make sure that this code is located within the directories that will be walked when an html page request is referred from Yahoo. If the files associated with the URLs linked from Yahoo Answers are in a directory *below* the one with this .htaccess file in it, it may be necessary to add

     RewriteOptions Inherit 

    after the Options directive above, and/or in the .htaccess files in those directories.

    Jim

    static

    8:38 am on Feb 4, 2009 (gmt 0)

    10+ Year Member



    jd, tried the new code, and it didn't show any change.

    About error logs, I'm not that familiar with checking these as I'm not a big time webmaster.

    I take it you are asking for my apache error logs from root? If so I looked around the net and couldn't find correct instructions on how to access them. As you can see I'm not the best with the command line, I just get on PuTTY from time to time.

    As an alternative, I went onto my whm/cpanel, and under Logs>Error Logs I found this

    [Wed Feb 04 03:20:08 2009] [error] [client 69.255.200.225] File does not exist: /home/admin/public_html/404.shtml
    [Wed Feb 04 03:20:08 2009] [error] [client 69.255.200.225] File does not exist: /home/admin/public_html/favicon.ico

    And it repeated for many many other IP's , obviously other visitors. I have no idea what these errors are or even if this is what your looking for? Does this shed any light on the situation?

    I also cleared my cache, with no luck. And made sure .htaccess is in the root directory or same place as the index.html file. Example of my files and their location
    /public_html/.htaccess
    /public_html/index.html
    /public_html/newpage.html

    So I'm fairly sure the .htaccess file is in the right location, correct me if I'm wrong.

    jdMorgan

    2:38 pm on Feb 4, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    That looks like the correct error log file. You should also be able to access it via FTP on most shared hosting set-ups.

    Neither line applies to the problem at hand, but I see that you have configured a custom 404 ErrorDocument in the control panel, but that that error document (/404.shtml) does not exist. Either disable the custom error document in your control panel, or create and upload a /404.shtml file.

    You also don't have a favicon for your site, and frankly, this is now "expected" -- If you cannot pick an appropriate image to use as a favicon.ico, then at least create a transparent one, so that these errors won't be clogging up your error log and polluting your site stats reports. Free "favicon makers" abound on the Web, and should be easy to find. The best ones will allow you to create a multi-resolution favicon file containing 16x16, 32x32, and 48x48-pixel images, so that the appropriate size can be used in the browser address bar and by Windows if a user drags the link for your site from the MSIE browser address bar onto their desktop for later use. Favicons are a nice feature for 'branding' your site, and they are well-worth the trouble to implement them.

    As for the main problem, indications are that mod_rewrite may not be active on your server. I'd suggest deleting all mod_rewrite rules in your /.htaccess file, and then trying a dirt-simple test:


    Options +FollowSymLinks
    RewriteEngine on
    #
    RewriteRule ^foo http://www.google.com/ [R=301,L]

    With that code in place, any request of any URL from your server that starts with "foo" should land you at Google.

    If that doesn't work, then a call to your host's help desk would be in order. Ask them if .htaccess files and mod_rewrite are supported on your server, and if so, ask them to look at your server config file and fix any problem that is preventing it from working.

    They will likely refuse to help you with your .htaccess code (because it costs them too much time/money, though they likely won't actually say that), so you should emphasize that you are not asking for help with the code, you are asking for help with your server configuration -- and likely with a problem that you cannot fix yourself using your control panel.

    Jim

    static

    6:45 pm on Feb 4, 2009 (gmt 0)

    10+ Year Member



    I'm not quite sure I understand what you'd like me to do here. You want me to put that code in my .htaccess file and test to see that it takes me to google when I type in my URL? Please explain further, especially what I'm suppose to have happen when I run this test.

    Samizdata

    10:04 pm on Feb 4, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    It's a simple test.

    1. Temporarily replace your current .htaccess with the code posted

    2. Point a browser at http://www.example.com/foo (where "example.com" is your site)

    3. If you don't arrive at Google's homepage, you don't have mod_rewrite enabled

    Hope this helps.

    ...

    static

    11:16 pm on Feb 4, 2009 (gmt 0)

    10+ Year Member



    Ok I tried and the google test code did redirect me to google.com

    Contacted my host which also runs their own datacenter, and they said mod rewrite was already on. I guess this test also confirms that

    Samizdata

    12:02 am on Feb 5, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    The test does indeed confirm it.

    Now replace that code with this code:

    # Set options
    Options +FollowSymLinks
    # Turn on mod_rewrite
    RewriteEngine on
    # If referrer contains "answers"
    RewriteCond %{HTTP_REFERER} answers [NC]
    # and referrer contains "yahoo"
    RewriteCond %{HTTP_REFERER} yahoo [NC]
    # Redirect any request to the new page
    RewriteRule /*$ http://www.example.com/newpage.htm [R=301,L]

    Replace "example.com" with your site and make sure that "newpage.htm" is in place on the server, clear your cache, then try the link from Yahoo Answers.

    If your browser does not have some software suppressing the referrer it should work.

    And you may have learned something.

    <Edit: reconsidered example>

    ...

    [edited by: Samizdata at 12:21 am (utc) on Feb. 5, 2009]

    static

    10:18 am on Feb 5, 2009 (gmt 0)

    10+ Year Member



    After trying the above new code, clicking the link in yahoo answers gave this error on Firefox:

    "Redirect Loop

    Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

    Using IE7 after clicking the link in yahoo answers it didn't give an error but never went to my website. The page just kept trying to load.

    Samizdata

    12:55 pm on Feb 5, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Apologies.

    Try inserting this as a final condition before the rule:

    # Prevent infinite loop
    RewriteCond %{REQUEST_URI} !^/newpage

    Where "newpage" is the name of your new page.

    ...

    jdMorgan

    1:57 pm on Feb 5, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Assuming that fixes the problem, the only difference (that I can see) between this code and the code I posted above is in the RewriteCond pattern(s) for HTTP_REFERER.

    So if this works, it would indicate that you did not post the correct HTTP_REFERER string value for Yahoo Answers referrals, which is why the original rule failed. The RewriteCond pattern(s) must match the expected input value, or the rule will never execute.

    If the correct HTTP_REFERER value (and its possible variants) can be determined, then it is very likely that only a single RewriteCond will be necessary to check it.

    Jim

    static

    2:53 pm on Feb 5, 2009 (gmt 0)

    10+ Year Member



    Sam: I added that rewritecond to prevent infinite loop and it did the trick. That code now works.

    JD: Regarding your version of the code you gave me, you say its really no different then the code you provided. What is the correct way to write the code for the HTTP_REFERER?

    I thank you both for your time and helping me on this .htaccess code

    Samizdata

    5:15 pm on Feb 5, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Glad it solved the problem, and apologies again for forgetting the infinite loop.

    Jim is right that a single condition should work, and his pattern is more specific (always good).

    I would guess that the problem lay with the first part of the RewriteRule - I used a wildcard, whereas other examples specified a target ending in "htm" or "html", which will presumably not work if the link from Yahoo Answers points to "/" instead.

    We can only offer suggestions based on what you tell us, and you may still want to consider using an internal rewrite rather than the external redirect I posted, for whatever reason.

    In my experience, Jim's code usually solves the problem as specified.

    I just muddle through as usual.

    ...

    jdMorgan

    6:05 pm on Feb 5, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    As pointed out, my original rewriterule pattern would NOT rewrite a request for "/". So here's a fix for that:

    Options +FollowSymLinks
    RewriteEngine on
    #
    RewriteCond %{HTTP_REFERER} ^[b]http://answers\.yahoo\.com[/b] [NC]
    RewriteCond %{REQUEST_URI} !^/newpage\.htm$
    RewriteRule ^([^/]+/)*([^\.]+\.html?)?$ /newpage.htm [NC,L]

    That will rewrite requests for .htm pages, .html pages, or "/" in any directory to /newpage.html.

    However, the main issue is that all of the characters in the first RewriteCond's pattern (shown here in bold) must match the beginning of the actual referrer string *exactly*. If it does not match the referrer string shown in your raw server access log, then the rule won't be invoked. And since we here can't see what's in your server access log file, you have to tell us -- We can't tell you...

    The RewriteCond referrer pattern can be adjusted to handle variations of the actual referrer header if needed -- for example, maybe Yahoo uses multiple sub-subdomains so that the "answers" workload can be shared among multiple hosts, and those sub-subdomains may therefore be present in the referrer header. But again, you'll have to tell us what the referrer variations are.

    Jim