How do I redirect a subdomain to a web site folder using an .htaccess file?

Our Hosting Control Panel treats subdomains and domains as equal entities for compatibility with many popular open-source applications, and there is no easy option to change this outside of managed or dedicated solutions. However, you may still want subdomains to redirect to subdirectories of your main web site domain as part of your presence, luckily it is possible with an .htaccess file.

  1. Add subdomain.yourdomainname.tld as alias to yourdomainname.tld
  2. Create an .htaccess file in yourdomainname.tld/public_html/ directory
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
    RewriteCond %{REQUEST_URI} !^/subdomain/
    RewriteRule (.*) /subdomain/$1‚Äč
  3. Create the subdomain directory web/yourdomainname.tld/public_html/subdomain and setup your index.html or web site

Additionally, you can use this code snipper for wildcard-style subdomains linking to your web site directories as well using this .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*?::\1
RewriteRule ^(.*)$ /%1/$1 [L]