3

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/
3
  • 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. – closetnoc Oct 9 '15 at 1:16
  • @closetnoc Essentially multiple sites (ie. domains) in multiple directories. But all hosted under a single hosting account. – MrWhite Oct 9 '15 at 11:42
  • @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!! – closetnoc Oct 9 '15 at 14:56
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!

1

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.

1

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.

4
  • You'll need to modify the code to rewrite all requests, not just the request for the document root (ie. no URL-path). You don't need to rewrite the requests for the "addon domains", since cPanel does that for you in the server config. – MrWhite Oct 9 '15 at 11:38
  • you're right about cPanel but cPanel also achieves this through .htaccess..once in the appropriate directory you can have relative links, so no need to rewrite all requests.. – 000 Oct 9 '15 at 14:13
  • 1
    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. – MrWhite Oct 9 '15 at 16:06
  • it will be a 404 Not Found error.. ;-) – 000 Oct 9 '15 at 16:33
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.