How can I host my main domain from a sub-folder using the .htaccess file?
In this article, we will explain how to host the main domain in a sub-folder using the .htaccess file.
By default, the main domain will use the public_html directory for its files.
Additional domains (addon, subdomains) will use subdirectories within the root directory.
However, it is possible to host the main domain from a subfolder within public_html by setting up a redirect in the .htaccess file in the public_html folder, so that the server redirects all requests for the main domain to the respective subfolder within public_html.
Website visitors will not know that the domain is loading from a subfolder, as this will not be added to the URL name.
To perform this operation, the following lines will need to be added in the file.htaccess, from public_html:
#Activate Rewrite mode
RewriteEngine on
#Replace "domeniu.ro" with the desired domain
RewriteCond %{HTTP_HOST} ^(www.)?domeniu.ro$
#Replace "subfolder" with the name of the subfolder used.
RewriteCond %{REQUEST_URI} !^/subfolder/
#Do not change these lines
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Replace "subfolder" with the name of the subfolder used.
RewriteRule ^(.*)$ /subfolder/$1
#Replace "domeniu.ro" with the desired domain
RewriteCond %{HTTP_HOST} ^(www.)?domeniu.ro$
#Replace "subfolder" with the name of the subfolder used and index.html with the desired default page.
RewriteRule ^(/)?$ subfolder/index.html [L]