2

In cPanel, I'm aware how to set document roots for domains, but I'm trying to have different document roots for a domain depending on the subdirectory.

       URL       |      Document Root
 example.com     | home/public_html/splash
 example.com/foo | home/public_html/apples
 example.com/bar | home/public_html/oranges
 foo.com         | home/public_html/splash 
 foo.com/foo     | home/public_html/apples
 foo.com/bar     | home/public_html/oranges

As you can see, there may be multiple domains that use the same subdirectory => document root scheme.

Any recommendations?

  • Maybe just terminology, but you can't actually change the DocumentRoot per subdirectory. The document root is, by definition, the root of all documents for a particular host and is defined in your server config (only). What is it you are trying to achieve? – MrWhite Mar 23 '15 at 9:58
  • I'm referring to document root within the cPanel context for addon domains. Document root in cPanel refers to the absolute path that a domain points to. – isUsername Mar 23 '15 at 18:35
1

There are many ways.

An easy way is via apache's mod_rewrite. For your example, you can use rules and files as follows:

Make an .htaccess file in /home/public_html/splash with the following contents:

RewriteEngine On
RewriteRule ^foo(.*)$ /apples$1 [L]
RewriteRule ^bar(.*)$ /oranges$1 [L]

That will work nicely for example.com and if you point foo.com to the same folder, then you're set.

Another way is to go in the linux shell and create symbolic links named foo and bar inside the splash folder (using the ln command) and make it point to /apples and /oranges respectively.

And for a cheap way out, you can create the folders named foo and bar inside public_html and create index files (index.html or index.php for example) and script it so it redirects to the correct folder, but that option depending on what you're doing might not fit your needs.

So the best case here is the first option. Use apache's mod_rewrite to map tails of URL's to folders just like I have done.

  • The immediate consideration is: What about duplicate content? I have other questions- but this one raises to the top of the list. – closetnoc Mar 23 '15 at 4:28
  • 1
    Sure, might cause duplicate content, might not. Has nothing to do with the users original question though. – nathangiesbrecht Mar 23 '15 at 15:52
  • @Mike Thanks for the recommendation. However, it results in the path being pointed to being a subdirectory of /splash. So, example.com/foo goes to home/public_html/splash/apples. While having example.com's document root being home/public_html/splash, I would like the rewrite for example.com/foo to go up one level and then to /apples. If it helps clarify, none of the folders are actually in the public_html root. They are in subfolders. However, /splash, /apples, and /oranges are all in the same directory. – isUsername Mar 23 '15 at 17:43
  • The .htaccess file being used here should be the one in the main domains document root ie. /home/public_html, not /home/public_html/splash. The directive to rewrite the URL to the /splash subdirectory (for anything other that /foo or /bar) should appear last to avoid conflict. – MrWhite Mar 23 '15 at 19:40
  • @w3d Thanks! That seems to be working much better. Do you have a recommendation for the regex for the last line to redirect to /splash? I tried using RewriteRule ^([^?]*)$ /splash [L,NC,QSA] but that resulted in a redirect loop. – isUsername Mar 24 '15 at 12:45

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.