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]

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]

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.


Do you have an ServerAlias demo.example.com for the VirtualHost that has this RewriteRule? If you don't, that is the reason it doesn't work.


If it's not a ServerAlias but a subdomain, remember to put your .htacces file in the subdomain, not in the main domain directory. Also add "RewriteEngine On" in .htaccess if you did not yet.

  • It depends on the way one has configured the subdomain. The same effect can be achieved with both a ServerAlias and separate VirtualHost for the subdomain. – Tero Kilkanen Jul 4 '14 at 10:39

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.