Problem getting rewrite from subdomain to directory to work

5,839

Solution 1

As it stands, you are redirecting to the same host. ie. demo.example.com/file is redirecting to demo.example.com/my-demo/file, not example.com/my-demo/file, which is going to result in a redirect loop. You need to specify an absolute URL in the RewriteRule substitution:

RewriteEngine On
RewriteCond %{HTTP_HOST} =demo.example.com
RewriteRule ^(.*)$ http://example.com/my-demo/$1 [R=301,L]

Solution 2

put this in .htaccess file in you're demo.example.com directory:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^demo\.example\.com [NC]
RewriteRule (.*) http://example.com/demo/$1 [L,R=301]

i.e: demo.example.com is in folder root-folder/demo
put that .htaccess file on folder demo, not on root-folder.

hope this work for you.

Share:
5,839

Related videos on Youtube

AlxVallejo
Author by

AlxVallejo

Updated on September 18, 2022

Comments

  • AlxVallejo
    AlxVallejo over 1 year

    I want to redirect demo.example.com to example.com/my-demo. This is my rewrite rule, but I'm not getting it to work on my production server:

      RewriteCond %{HTTP_HOST} ^demo\.example\.com
      RewriteRule ^(.*)$ /my-demo/$1 [R=301,L]