How to change document root of main domain in cPanel shared host?

9,980

Solution 1

Sorry my question is actually wrong because I had the misconception that all document roots must live inside public_html/. I just moved them outside, and now public_html/ is just the doc root for my main domain. So I avoided to have scattered files and every site is in its own dir.

Solution 2

For changing the main/primary domain, you will need to have root SSH access and be able to locate and edit the following file (replacing your user & domain info):

/var/cpanel/userdata/USERNAME/DOMAIN.COM
  1. Once you have opened the file, look for the following line:

    documentroot: /home/USERNAME/public_html

  2. Modify the location according to your needs. Save it and exit.

  3. Rebuild the Apache conf and restart Apache:

    /scripts/rebuildhttpdconf

    service httpd restart

The change will be immediate. Simply clear your browser cache and force refresh the page!

Solution 3

In httpd.conf (which is usually located in /usr/local/apache/conf in a linux box with cpanel), you'll find a number of sections beginning with <virtualhost. Inside each one you'll see a line for "DocumentRoot".

The path shown next to it is what the system will use to find an index file when a user tries to access the domain name itself as a url.

Also, to confirm you're dealing with the right domain, look for "ServerName" entries or even the domain name itself. After all, each individual block of text beginning with <virtualhost contain settings specific to only one domain.

When the settings are changed, restart apache gracefully. You may want to follow the instructions at the top of the configuration file that cpanel puts in so that you can retain the changes in the configuration.

Another option which is less flexible is to try to enter user account management or even the tweak settings in the WHM, but I don't play with WHM too much since I prefer the shell.

If you don't want to play with apache configuration too much, then what you could do is create a folder where you want the documents to be located and create a symbolic link.

In linux, you'd type:

ln -s /path/to/new/folder /home/domain

where "/home/domain" is your current document root for the domain you want to change. Just make sure you set the permissions of the symbolic link and the new folder correctly or users will find their access being denied.

As always, test your setup.

Solution 4

You can add an .htaccess file if not existing previously.

If the file already exists then add below code to it; Don't overwrite previously existing .htaccess file without taking BACKUP or renaming.

public_html/
   --.htaccess
   --maindomain_root/
   --othersite1_root/
   --othersite2_root/

Content for .htaccess

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteRule ^(/)?$ maindomain_root [L]

RewriteCond %{HTTP_HOST} ^(www.)?othersite1.com$
RewriteRule ^(/)?$ othersite1_root [L]

RewriteCond %{HTTP_HOST} ^(www.)?othersite2.com$
RewriteRule ^(/)?$ othersite2_root [L]

Please note one important thing - Having addon domains which are non-related in a fashion listed above will lead to mixed urls in response to search engine queries UNLESS you define your indexable content and sitemap carefully.

Share:
9,980

Related videos on Youtube

Antonis Gr
Author by

Antonis Gr

Updated on September 18, 2022

Comments

  • Antonis Gr
    Antonis Gr over 1 year

    I am in a shared host that I have basic cli access.

    Inside public_html/ I host several addon domains, each in its own dir. Is it possible to make my main domain's document root inside another folder to achieve the following structure?

    public_html/
       --maindomain_root/
       --othersite1_root/
       --othersite1_root/
    

    instead of:

    public_html/
       --maindomain_file1
       --maindomain_file2 (..etc)
       --othersite1_root/
       --othersite1_root/
    
    • closetnoc
      closetnoc over 8 years
      Do you mean have a single site in multiple directories?? If so, Why do you want to do this? Is it for personal organization? You can create symlinks from your existing maindomain directory to other directories if you like but it would be accessed like maindomain/mysymlink/myfile. Otherwise, I do not think so, but I am not willing to take any bets just yet.
    • MrWhite
      MrWhite over 8 years
      @closetnoc Essentially multiple sites (ie. domains) in multiple directories. But all hosted under a single hosting account.
    • closetnoc
      closetnoc over 8 years
      @w3d Yes. Of course. I understand this. However, now that I have looked at it again, it appears he is just moving the one site up a directory. I misread it as being more than one directory for the single site. Ooopppsss!!
  • MrWhite
    MrWhite over 8 years
    Well, given your directives above, what would you expect to happen to a request for http://maindomain.com/path/to/file or http://maindomain.com/assets/styles/default.css? cPanel does not normally use .htaccess to manage AddOn domains, only if you state that you want an external redirection.
  • 000
    000 over 8 years
    it will be a 404 Not Found error.. ;-)