4

I am having a problem making a sub directory act as the public_html for my main domain, and getting a solution that works with that domains sub directories too.

Background

My hosting allows me to host multiple sites, which are all working great. I have set up a subfolder under my ~/public_html/ directory called /domains/, where I create a folder for each separate website. The folder structure on my server looks something like this:

  • public_html
    • domains
      • websiteone
      • websitetwo
      • websitethree
      • ...

This keeps my sites nice and tidy. The only issue was getting my "main domain" to fit into this system. It seems my main domain, is somehow tied to my account (or to Apache, or something), so I can't change the "document root" of this domain. I can define the document roots for any other domains ("Addon Domains") that I add in cPanel no problem. But the main domain is different.

The problem

I was told to edit the .htaccess file, to redirect the main domain to a subdirectory. This seemed to work great, and my site works fine on it's home/index page.

The problem I'm having is that if I try to navigate my browser to say the images folder (just for example) of my main site, like this:

www.yourmaindomain.com/images/

then it seems to ignore the redirect and shows the entire server directory in the url, like this:

www.yourmaindomain.com/domains/yourmaindomain/images/

It still actually shows the correct "Index of /images" page, and shows the list of all my images. It's just the "pretty" URL that's the problem.

Example of my .htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteCond %{REQUEST_URI} !^/domains/yourmaindomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domains/yourmaindomain/$1

RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ domains/yourmaindomain/index.html [L]

Does this htaccess file look correct? I just need to make it so my main domain behaves like an addon domain, and it's subdirectories adhere to the redirect rules.

Adam Harte
  • 10,369
  • 7
  • 52
  • 85
  • Should probably be moved to serverfault. – Marian Jun 07 '10 at 20:49
  • Did you end up solving this with virtual hosts (as per the accepted solution)? I have this exact same problem, but CANNOT use virtual hosts on my shared hosting - so unfortunately, the solution doesn't actually solve the issue for me. – J23 Mar 14 '16 at 20:33

2 Answers2

1

Don't use mod_rewrite for that, use virtual hosts for that. I won't explain that here in detail, you'll find all the necessary information at the above link. (Hint: You'll need the name-based ones probably.)

You probably want to move your stuff out of ~/public_html/ to some more generic place like /var/www/ or /srv/www.

Besides that: Don't use .htaccess files on production.

Marian
  • 5,817
  • 2
  • 18
  • 21
  • Are virtual hosts possible on shared hosting? – Adam Harte Jun 07 '10 at 21:09
  • That depends on your shared hosting setup, you should ask your provider about that. – Marian Jun 07 '10 at 21:28
  • @Marco: Just wanted to say it, if he needs to use them he needs to use them. As the first sentence on the page I linked to says “In general, you should never use .htaccess files unless you don't have access to the main server configuration file.” – Marian Jun 17 '10 at 17:02
  • I have this same question, but cannot use virtual hosts on my shared hosting. Would really would've liked a mod_rewrite-based solution... – J23 Mar 14 '16 at 20:32
0

I have it organized the same way yo do. Had the same issue with my main domain. This .htaccess works for me and even solves the ugly URL thing.

# .htaccess main domain to subdirectory redirect 

RewriteEngine on 
# Change example.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/subdirectory/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /subdirectory/$1 
# Change example.com to be your main domain again. 
# Change 'subdirectory' to be the directory you will use for your main domain 
# followed by / then the main file for your site, index.php, index.html, etc. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ subdirectory/ [L]
Bernard
  • 607
  • 7
  • 11