Host Domain from a Sub Folder using HTACCESS – Opensite - Digital Solutions Agency

0422 374 731 info@opensite.com.au

Host Domain from a Sub Folder using HTACCESS

Last update:

Tutorials , ,

By default your domain will use the public_html folder to host your websites files. This can cause a problem, because in most cases addon domains and/or sub domains will also be located in the public_html folder. My prefered way to deal with this is to put each website in it’s own self containing folder in public_html.

For example:


public_html/primary-website.com.au/

public_html/addon-website.com.au/

To achieve this for your primary domain you will need to edit the .htaccess file in the public_html folder. Copy and edit the following into .htaccess.


# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change the following two lines
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.html [L]