How to redirect subdomain to folder using htaccess

Vesta Control Panel equally treats subdomains and domains and there is no option to change this behaviour. However you still might need to handle subdomains as subdirectories of your main domain. Luckily, it can be easily done with .htaccess


  1. Add subdomain.domain.com as alias to domain.com

  1. Create following .htaccess file in domain.com/public_html/ directory
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteCond %{REQUEST_URI} !^/subdomain/
RewriteRule (.*) /subdomain/$1
  1. Create subdomain directory web/domain.com/public_html/subdomain and put index.html

Actually you can handle wildcard subdomains too. Here is .htaccess for that

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