Domains and DNS

Host the account's main domain in a subdirectory

Reviewed on 2 min read main domain directories

The main domain in cPanel uses public_html. If you want to serve its files from a subfolder while keeping the public URL, you can use an internal rewrite. First, check whether the application has its own procedure for being installed this way.

Prepare the folder

Back up the .htaccess file and the website. Put the application in the folder you've chosen, for example public_html/subdirectorio, and check its entry file. The example below uses index.php; change that name to index.html if your website needs it.

Don't add it without reviewing the rules of WordPress or another CMS: their routing may need a different configuration.

Rewrite example

In the .htaccess file in public_html, before the application's general rules:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ subdirectorio/index.php [L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdirectorio(?:/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ subdirectorio/$1 [L]

Replace the domain, keeping the dots escaped with \., and change subdirectorio to your folder. The hostname conditions stop the example from applying to other domains that share that .htaccess.

The first rule handles the home page. The others send paths to the subfolder, except those already inside it and files or directories that exist directly in public_html. So that existing content may still be public.

Check and revert

Test the home page, internal paths, static assets, forms and the admin area. Check that the browser's address bar keeps the expected URL and that the application doesn't add the subfolder or get stuck in a loop.

If it fails, restore the previous .htaccess and check the application's own procedure. This technique organises how the site is published; it doesn't hide files, replace access control or isolate websites from each other.

You may also find useful

Something wrong or out of date? Tell us and we will review it.