I want to redirect specific wildcard subdomain to a specific URL on another domain. Both domains are on the same server/public_html, and redirect all the inner pages of the wildcard subdomain to the inner pages of the other domain.
For example:
music.example
to another.example/music/
and
redirect music.example/happy-birthday/
to another.example/happy-birthday
redirect music.example/sing-to-you/
to another.example/sing-to-you/
I have more than 100k posts so I cannot make the redirection of the inner pages one by one using .htaccess
.
I want to redirect specific wildcard subdomain to a specific URL on another domain. Both domains are on the same server/public_html, and redirect all the inner pages of the wildcard subdomain to the inner pages of the other domain.
For example:
music.example
to another.example/music/
and
redirect music.example/happy-birthday/
to another.example/happy-birthday
redirect music.example/sing-to-you/
to another.example/sing-to-you/
I have more than 100k posts so I cannot make the redirection of the inner pages one by one using .htaccess
.
You can do something like the following at the top of your .htaccess
file in the document root using mod_rewrite before the WordPress front-controller.
Note that since you have two distinct patterns (root directory to /music
and everything else to the root directory on the other domain) then you'll need to two rules.
RewriteEngine On
# "music.example/" to "another.example/music/"
RewriteCond %{HTTP_HOST} ^music\.example\ [NC]
RewriteRule ^$ https://another.example/music/ [R=302,L]
# "music.example/<something>" to "another.example/<something>"
RewriteCond %{HTTP_HOST} ^music\.example\ [NC]
RewriteRule (.+) https://another.example/$1 [R=302,L]
The RewriteCond
(condition) directive checks the requested host. The RewriteRule
naturally redirects to the other domain. In the second rule the URL-path from the request is captured in the $1
backreference.
Note that this is a 302 (temporary) redirect. If this is intended to be permanent, then change to 301 (ie. R=301
) but only once you have confirmed that it works OK. 301s can be problematic when testing as they are aggressively cached by the browser.