Forum Moderators: open
I'm trying to redirect all pages on my domain to a script on another domain for certain useragents. So in my .htaccess I have
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^Testingbot.*
RewriteRule ^(.*)$ http://66.66.66.66/cgi-bin/foo.pl/$1 [L]
The script foo.pl is actually a cache script and the idea is to cache a dynamic site for search engine spiders.
However, when using wget, for example, I get nasty behaiviour:
$wget -UTestingbot http://www.foo.com/xyz.html
--15:34:48-- http://www.foo.com/xyz.html
=> `xyz.html'
Resolving www.foo.com ... done.
Connecting to www.foo.com[66.66.66.65]:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://66.66.66.66/cgi-bin/foo.pl/xyz.html [following]
--15:34:48-- http://66.66.66.66/cgi-bin/foo.pl/xyz.html
=> `xyz.html'
Connecting to 66.66.66.66:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html] [ <=> ] 15,786 15.05M/s
15:34:48 (15.05 MB/s) - `xyz.html' saved [15786]
Now, I've done this before using
RewriteRule ^(.*)$ /cgi-bin/foo.pl/$1 [L]
I've read the mod_rewrite docs but couldn't find anything about doing internal redirects to other ips...
Thanks for any help!
A redirect to another IP address *is* an external redirect by definition. If the second IP address resolves to the same server, and it is set up so that both virtual servers share filespace, then I suppose you could implement it as an internal URL rewrite, but in that case, IP addresses have nothing to do with it.
Otherwise you might want to look into using mod_proxy and the proxy function of mod_rewrite. See the RewriteRule [P] flag.
Jim
RewriteRule ^//*(.*)$ http://66.66.66.66/cgi-bin/foo.pl/$1 [P]
The [P] option wasn't working for me before but adding the //* seems to have helped.
Docs:
[httpd.apache.org...]
rule:
^/somepath(.*) http://otherhost/otherpath$1 [P]result:
http://otherhost/otherpath/pathinfo
via internal proxy
Now I get the desired behaivour using wget, i.e. no code 302 .. just a seamless rewrite.
RewriteRule ^(.*)$ /cgi-bin/foo.pl/$1 [P]
The thing in my other post was just breaking the redirect in a way which I wouldn't notice it. I tested the above by making the cache script write some extra text to the page to make sure it was actually going through the cache script.