August 17, 2020

How to set up htaccess to use a parked domain as itself

The problem: You pay for hosting of one domain but you would also like to serve other domains that you have parked there, their files being in subdirectories of the account domain.

The solution: I can only vouch for my experience with Apache, where what is working for me is:

1. In the htaccess file of your account domain:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^.*parkeddomain\.com
    RewriteRule ^(.*)$ /parkeddomain/$1 [L]

2. In the htaccess file of your parked domain:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^.*parkeddomain\.com
    RewriteCond %{REQUEST_URI} \/parkeddomain\/
    RewriteRule ^(.*)$ http://www.parkeddomain.com/$1 [R=301,L]

Brief explanation: The first instruction rewrites the requests for your parked domain to the appropriate subdirectory. That would appear to be enough, but the files in that subdirectory can also be loaded by the account domain and any other parked domain if the path is designated, i.e., http://accountdomain.com/parkeddomain/ and http://parkeddomain2.com/parkeddomain/. So the second instruction prevents this by redirecting the requests for the subdirectory back to the parked domain if they are for any other domain.

Note: Any subdirectory of the parked domain that has the same name as a subdirectory of the account domain, such as “image”, must also have an htaccess file with the directive “RewriteEngine on”. And remember while testing to use your browser’s developer tools to disable caching.