htaccess - Redirect Specific Wildcard Subdomain to a specific URL on another domain

admin2025-06-02  1

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 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:

  • redirect 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.

Share Improve this question edited Mar 12, 2019 at 14:51 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Mar 12, 2019 at 6:58 XATAXATA 6712 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748814829a313948.html

最新回复(0)