Forum Moderators: phranque

Message Too Old, No Replies

Use mod rewrite to redirect to images on another server

         

talkingmuffin

10:05 pm on Jul 21, 2010 (gmt 0)

10+ Year Member


Howdy all!

I would like to take all of the requests going to a particular directory (cgi-bin) and redirect them to the exact same directory on another server. Urls will be coming into apache like this:

new.server.com/cgi-bin/acdis.dll?cmd=see&fp=/is-pix/D01019.jpg&h=350&w=350

I would like to redirect them to:

old.server.com/cgi-bin/acdis.dll?cmd=see&fp=/is-pix/D01019.jpg&h=350&w=350

I've tried the following:

RewriteRule ^cgi-bin(.*)$ http://old.server.com/cgi-bin$1 [R]

But this simply gives me a 404 on the new.server.com.

Thanks a bunch for your help!

jdMorgan

7:28 am on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is likely that 'cgi-bin' on new.server.com is an alias. Therefore, the Alias directive is applied before your code can execute.

Either that, or perhaps you have not set-up and enabled mod_rewrite. Adding that set-up, and assuming that this code goes into new.server.com/.htaccess, you'd get something like:

Options +FollowSymLinks
RewriteEngine on
#
RewriteRule ^cgi-bin/(.*)$ http://old.server.com/$1 [R=302,L]

Query strings pass through RewriteRules by default.

If your cgi-bin directory is Aliased, you'll need to re-code the server config file to remove that Alias and do the redirect instead.

Jim

talkingmuffin

5:01 pm on Jul 23, 2010 (gmt 0)

10+ Year Member



Jim,

That solved my problem. Thank you so much for your help.

Cheers.

jdMorgan

6:02 pm on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assumed you'd want a 302, because you will eventually stop redirecting these requests to old.server.com.

If that is not the case, then change the code to do a 301 to specify a permanent move. Or, if you're not at all sure what the final disposition of these URLs will be, you could use a 303-See Other response, which has no associated temporary/permanent semantics whatsoever - and has never had any such semantics.

See the HTTP/1.0 and HTTP/1.1 protocol specifications for an explanation of 301, 302, 303, and HTTP/1.1-only 307 responses, and note the very-confused semantics of 302, which has been redefined in HTTP/1.1 over HTTP/1.0, with 307 taking the specific meaning of "Moved Temporarily" for use with HTTP/1.1 transactions only.

Jim