If both the main domain and subdomain point to the same directory on the filesystem, then you need to check the requested host using a mod_rewrite condition.
So, for example, the following directives should go before the WordPress front-controller (ie. before the # BEGIN WordPress
section):
RewriteCond %{HTTP_HOST} ^(example\.com) [NC]
RewriteRule ^(music/end/)$ http://music.%1/$1 [R=301,L]
The use of backreferences in the RewriteRule
substitution is simply to prevent repetition. %1
is a backreference to the captured group in the last matched CondPattern (ie. example.com
) and $1
is a backreference to the captured group in the RewriteRule
pattern (ie. music/end/
).
You need to use mod_rewrite (as opposed to a mod_alias Redirect
) since you most probably have existing mod_rewrite directives associated with WordPress, and there could be conflicts otherwise. Besides, you can't check the Host
header using mod_alias.
You will need to clear your browser cache before testing.
UPDATE: From your comment...
RewriteRule ^ng-music/(.*) https://music.example.com/music/$1 [R=301,L]
This does something different to the "desired" redirect as stated in your question. By itself, this shouldn't result in a redirect loop.
RewriteRule ^ng-music/(.*) https://music.example.com/music/$1 [R=301,L]
that is the htaccess code i tried.end
is variable?