Forum Moderators: phranque
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?
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
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.
The correct referrer pattern is
^http://answers\.yahoo\.com [NC]
therefore, that should have worked. But...
Jim
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.
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?
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.
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)?
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]
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 Jim
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.
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]
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
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]
"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.
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
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
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.
...
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]
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