Forum Moderators: open

Message Too Old, No Replies

Internal redirect to external site

mod_rewrite confusion

         

physics

10:23 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

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]

instead of what I have, i.e. rewriting to the same domain. That worked. But right now I really do want to do an internal redirect to this other ip.

I've read the mod_rewrite docs but couldn't find anything about doing internal redirects to other ips...

Thanks for any help!

jdMorgan

10:47 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



physics,

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

physics

10:51 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, answered my own question. The answer in the docs but wasn't working because of a different (apparently) unrelated syntax error. This is what worked:

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.

physics

10:54 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the reply jdmorgan... we must have been writing at the same time.

physics

12:38 am on Jul 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops! Sorry, what actually works is

RewriteRule ^(.*)$ /cgi-bin/foo.pl/$1 [P]

With the proxy module loaded in httpd.conf (had to recompile apache with proxy module also).

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.