billytwoshoes
billytwoshoes

Reputation: 11

Redirect list of subdomains to new URL

I have many subdomains on oldsite.example that I want to 301 redirect to newsite.com. However some subdomains should stay at oldsite.example and the redirected subdomains follow no regex-able pattern, making wildcards irrelevant.

I'd like an expression where I can write a list something like sub1,page,boop which would set up redirects for all the following

sub1.oldsite.example -> sub1.newsite.example
page.oldsite.example -> page.newsite.example
beep.oldsite.example -> beep.newsite.example

Upvotes: 0

Views: 48

Answers (1)

Stephen Ostermiller
Stephen Ostermiller

Reputation: 25524

Here is a rewrite rule that can be used to do this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(sub1|page|boop)\.oldsite\.example$ [NC]
RewriteRule ^/?(.*)$ https://%1.newsite.example/$1 [R=301,L,QSA]
  • RewriteEngine on enables rewrite rules and only needs to be used once in .htaccess
  • RewriteCond specifies a condition that needs to be met before the rewrite rule is activated
  • %{HTTP_HOST} is the domain name
  • (sub1|page|boop) your list of subdomains in a capturing group
  • [NC] - case insensitive
  • ^/?(.*)$ matches every page on those subdomains with a capturing group
  • https://%1.newsite.example/$1 the new URL with substitutions of the capturing groups from the condition and the rule
  • [R=301] - 301 permanent redirect
  • [L] - Last rewrite rule (so that others don't get triggered after this one matches)
  • [QSA] - Query string append - preserve any parameters on the URL

Upvotes: 1

Related Questions