Mobile app version of vmapp.org
Login or Join
Lee4591628

: Problem getting rewrite from subdomain to directory to work 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

@Lee4591628

Posted in: #301Redirect #Apache #Htaccess #ModRewrite

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/ [R=301,L]

10.04% popularity Vote Up Vote Down


Login to follow query

More posts by @Lee4591628

4 Comments

Sorted by latest first Latest Oldest Best

 

@Smith883

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


RewriteEngine On
RewriteCond %{HTTP_HOST} ^demo.example.com [NC]
RewriteRule (.*) example.com/demo/ [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.

10% popularity Vote Up Vote Down


 

@Alves908

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 ^(.*)$ example.com/my-demo/ [R=301,L]

10% popularity Vote Up Vote Down


 

@Jessie594

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.

10% popularity Vote Up Vote Down


 

@Hamaas447

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme