Warwick Warwick - 15 days ago 7
Apache Configuration Question

Redirect anything in a specific directory to a different domain

I've just upgraded to a WordPress website and moved my old ASPX files to a different domain. Now I need anything in

http://www.example.com/subdir/$
to redirect to
http://a.example.com/subdir/$
. Everything else must remain, of course.

The closest I have gotten is placing the following in a sub-directory called
subdir
but this redirects
http://www.example.com/subdir/one/two.aspx
to
http://a.example.com/one/two.aspx
– strips the
subdir
part.

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdir$
RewriteRule ^/?subdir/(.*)$ http://a.example.com/subdir/$1 [R=301,L]

Answer

Use this RewriteRule in your main (parent) .htaccess:

RewriteRule ^subdir/(.*)$ http://a.example.com/subdir/$1 [L,R=301]