Htaccess rewrite subfolder to subdomain

i have a domain with many subdomain; what i want to do is to map specific subdomains do specific subfolder.

let's suppose the domain is example.com, i have these domains:

  • example.com
  • www.example.com
  • demo1.example.com
  • demo2.example.com

and 2 subfolders in my var/www/html folder:

  • /var/www/html (for example.com and www.example.com)
  • /var/www/html/demo1 (for demo1.example.com)
  • /var/www/html/demo2 (for demo2.example.com)

So what i want to is set up the correct mapping so that:

  • opening (www.)example.com/demo1 should redirect to demo1.example.com
  • opening (www.)example.com/demo2 should redirect to demo2.example.com
  • opening (www.)example.com/anything-else should do nothing (content in /var/www/html)

Obviusly demo1.example.com should use the content in /var/www/html/demo1 and demo2.example.com should use the content in /var/www/html/demo2

1 answer

  • answered 2019-08-14 19:38 arkascha

    You probably want to implement rules handling both directions: 1. redireting requests to the "www" host to the "subdomain" (actually another host name) 2. rewriting requests to the non-www hosts to a folder, if such folder exists

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
    RewriteRule ^/?demo1/(.*)$ https://demo1.example.com/$1 [R=301]
    RewriteRule ^/?demo2/(.*)$ https://demo2.example.com/$1 [R=301]
    
    RewriteCond %{HTTP_HOST} ^demo1\.example\.com$
    RewriteRule ^ /demo1/%{REQUEST_URI} [END,QSA]
    RewriteCond %{HTTP_HOST} ^demo2\.example\.com$
    RewriteRule ^ /demo2/%{REQUEST_URI} [END,QSA]
    

    You could certainly implement a more generalized rule for the second part (the internal rewriting), but above explicit approach is easier to reach, thus easier to maintain. I prefer it for that reason.

    It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...

    In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

    These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

    And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).