Carrlos Boyd
Carrlos Boyd

Reputation: 147

Redirect old subdomain to new subdomain

I am trying to do the following redirects of an old subdomain to a new subdomain, respectively:

sub.mydomain.com/d/(all_files),
sub.mydomain.com/d2/(all_files)

redirect to

sub2.mydomain.com/d/(all_files),
sub2.mydomain.com/d2/(all_files)

There are other folders/files in the original "sub" that will not be redirected. Only the files in the "d" and the "d2" folders need redirecting.

Upvotes: 2

Views: 668

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270677

Use RewriteCond to test for the HTTP_HOST:

RewriteEngine On
# If it matches sub. the old subdomain,,,
RewriteCond %{HTTP_HOST} ^sub\.
# Redirect URLs starting with d/ or d2/ into sub2.mydomain.com
RewriteRule ^(d2?)/(.*)$ http://sub2.mydomain.com/$1/$2 [L,R=301]

The expression ^(d2?) is:

  • ^ start of the string
  • d followed by an optional 2 (? means previous expression is optional)
  • () whole thing is captured for reuse in $1

Upvotes: 2

Related Questions