I have set up virtual document root using vhost_alias and it works good if directory of subdomain exists. but it throws 403 error if subdomain dir doesnt not exist. I want to set a default dir if sub domain doesn't exist.
current vhost conf
<VirtualHost *:80>
ServerAlias *.domain.com
VirtualDocumentRoot /home/ubuntu/www/domain/%1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "home/ubuntu/www/domain/*">
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
I tried using error document
ErrorDocument 403 http://app.domain.com%{REQUEST_URI}?%{QUERY_STRING}
but it changes browser url. Is there any way to redirect internally? i have also tried rewrite but that doesn't seems to work.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT} !-d
RewriteRule %{DOCUMENT_ROOT} "/home/ubuntu/www/domain/app" [QSA,L]
Found solution
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT} !-d
RewriteRule (.*) http://app.domain.com/%{REQUEST_URI} [P,L]
Adding this inside Directory directive solved my problem. For SSL sites include this in Virutalhost block
SSLProxyEngine on
ProxyPassReverse / https://app.domain.com/