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:

  • redirect music.example.com to another.example/music/


  • redirect music.example.com/happy-birthday/ to another.example/happy-birthday

  • redirect music.example.com/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.com/" to "another.example/music/"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule ^$ https://another.example/music/ [R=302,L]

# "music.example.com/<something>" to "another.example/<something>"
RewriteCond %{HTTP_HOST} ^music\.example\.com [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.

  • Just to add... note that this is a 302 (temporary) redirect. If this is intended to be permanent, then change to 301. Testing with 302 is preferred as it avoids caching issues.
    – MrWhite
    Mar 12 '19 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.