1

I have a domain and subdomain example.com and music.example.com and they are both on root directory /public_html

I want to redirect example.com/music/end/ to music.example.com/music/end/but since the both of them are in root directory it is giving me redirection error.

Please is there any way i can make 301 redirection from the old url to the new one without getting errors

3
  • Please share your current directives that are giving you this error. And specifically, what kind error are you getting; a redirect loop?
    – MrWhite
    Commented Jun 29, 2018 at 0:18
  • RewriteRule ^ng-music/(.*) https://music.example.com/music/$1 [R=301,L] that is the htaccess code i tried. Commented Jun 29, 2018 at 0:26
  • Curious, that directive doesn't really relate to the desired redirect you stated in the question? You are also changing the URL-path with that directive. In my answer below I've assumed the URL-path is passed through unchanged - as in your example. Is this not the case? Your directive also suggests that end is variable?
    – MrWhite
    Commented Jun 29, 2018 at 0:31

1 Answer 1

1

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.

4
  • but what if i have more than 12 url to redirects Commented Jun 29, 2018 at 0:43
  • Glad you got it working. The browser cache can be a problem when testing 301 (permanent) redirects. As the browser is just as likely to cache the errors. It is often recommended to first test with 302 (temporary) redirects - that should not be cached by default - and change to 301 once you have confirmed it works OK.
    – MrWhite
    Commented Jun 29, 2018 at 0:44
  • You can repeat the above 12 times, or if there is a pattern, then you can create the appropriate regex to cover all 12 URLs? There are some other tricks like skipping directives, but it would be preferable to look for a pattern to these 12 URLs.
    – MrWhite
    Commented Jun 29, 2018 at 0:48
  • LOL, thanks a lot I appreciate your time and effort. Commented Jun 29, 2018 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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