Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]
share|improve this question

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]
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
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

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|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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