views:

34

answers:

2

I want to redirect one specific url like:

www.example.com/test/ex.gif

to

static.example.com/ex.gif

How can i do this in .htaccess?

+2  A: 

From the top of my head:

RewriteRule ^http://www.example.com/test/ex.gif$ http://static.example.com/ex.gif [NC,R=301]

or to any file in test:

RewriteRule ^http://www.example.com/test/(.*)$ http://static.example.com/$1 [NC,R=301]

Corrected after advice from Mr. Berardi

Jonas Elfström
Actually the example you have is wrong. You don't want (.*) in front of the path, because then anything like /blah/blah/blah/text/ex.gif will also be caught. You just want RewriteRule ^/test/(.*)$ http://static.example.com/$1 [NC,R=301]. Notice that the "L" is also not necessary because a redirect automatically breaks out of the rewrite rules.
Nick Berardi
Yes, that could be a problem. I thought that ^ is the start of line anchor and if so I'm having a hard to time to see how ^/test/ could match. Please enlighten me.
Jonas Elfström
+2  A: 

Try this mod_rewrite rule:

RewriteEngine on
RewriteRule ^test/(.*) http://static.example.com/$1

Or this mod_alias directive:

Redirect /test/ http://static.example.com/
Gumbo