By default (unless modified by the system administrator), your website's files should be stored within the /public_html/ directory.

This is considered the web root for your primary domain name.

Files in this location are what cPanel offers when someone types your main domain (the one you provided when you signed up for hosting).

So this is where you upload or put your website files.

Addon and subdomain document roots are also restricted to the public_html of the account under which they are created.

But what if you want to serve content in the folder /public_html/$folderName as your primary domain content and for some reason do not wish to push the files to "public_html"?

That is, you want to point your primary domain to the contents of /public_html/$folderName.

If you are using a shared server and thus do not have access to make root changes, there is a little trick to get this done.

 

Create A Folder

Create the folder " /$folderName " (if it doesn't already exist) that will serve the files of the primary domain:

with cPanel File Manager (cPanel > Files >> File Manager >>> public_html):

  • Click + Folder in the toolbar.
  • Enter the New Folder Name: text box when the new interface appears.
  • Enter the location in which the system will create the item in the New folder will be created in: text box.
  • Click Create New Folder.

with SSH Client or cPanel Terminal ( Advanced >> Terminal for cPanel ), ensure you are in the correct directory (pwd), then:

mkdir -p public_html/$folderName

 

Backup Existing .htaccess

Before making any change to a file in your cPanel, always back up the existing file.

In this case, it is highly recommended that you back up your .htaccess file before proceeding.

It is also worth noting that by default, hidden files (dotfiles) are not displayed in File Manager.

To see any existing dotfile, you have to use File Manager's "Show Hidden Files (dotfiles)" option to view the hidden files.

  • If you haven't already, log in to cPanel as the cPanel user.
  • Click on the "File Manager" icon.
  • Click on the "Settings" button in the upper right corner.
  • Check the "Show Hidden Files (dotfiles)" checkbox.
  • Click the "Save" button.

with an SSH Client or cPanel Terminal, run the command below:

ls -al

This will list all of the hidden files and directories in your working directory.

To make a copy:

cp .htaccess htaccess.backup

or:

cp .htaccess htaccess-backup.txt

Then try something like this to see if it will work for you:

  
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www.)?webcomm.dev$
    RewriteCond %{REQUEST_URI} !^/testing/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /testing/$1
    RewriteCond %{HTTP_HOST} ^(www.)?webcomm.dev$
    RewriteRule ^(/)?$ testing/index.html [L]
  

To download this directive, please https://cdn.webhostingmagic.com/code-examples/serve-sub-folder-content.htaccess.txt

In the above, we are using "webcomm.dev" as your domain.

And "testing" as the $folderName

Try this out and if everything works out, you will see that your cPanel primary domain is now serving the contents of the sub-folder.

If for any reason that didn't work, either comment out the rules or move back the backed up .htaccess to replace the existing one.

To do via a command line:

mv htaccess-backup.txt (or htaccess.backup) .htaccess

To do that via cPanel's File Manager:

Click on the name of the file. E.g htaccess-backup.txt

Enter the new name (.htaccess) in the text box.

Press the Return key to save the change.

Please remember that there are many ways to accomplish this and a good web search and testing should be able to get you the result you need.

 

ATTENTION:

When Apache reads a .htaccess file, the rules are applied recursively to any sub-directories from where the rule was found.

So a .htaccess located in the path specified /$username/public_html/.htaccess will also affect any of the sub-directories located within "public_html":

\__testing
\__sub-domainFolder
\__blog
Was this answer helpful? 0 Users Found This Useful (0 Votes)