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 as alias to

  1. Create following .htaccess file in directory
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteCond %{REQUEST_URI} !^/subdomain/
RewriteRule (.*) /subdomain/$1
  1. Create subdomain directory web/ 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]