Primary host domain from a subfolder

I had a problem in that the subdirectory acts like public_html for my main domain and gets a solution that also works with similar domain directories.

Background

My hosting allows me to host several sites that work great. I created a subfolder under my ~ / public_html / directory / domains / , where I create a folder for each individual site. The folder structure on my server looks something like this:

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

It makes my sites nice and neat. The only problem was that my "primary domain" fit into this system. It seems that my main domain is somehow attached to my account (or to Apache or something else), so I can not change the "document root" of this domain. I can determine the roots of the document for any other domains ("Addon Domains") that I add to cPanel without any problems. But the main domain is different.

Problem

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

The problem that I encountered is that if I try to go to my browser to say a folder with images (for example) of my main site, for example:

www.yourmaindomain.com/images/

then it seems to ignore the redirect and show the entire server directory in the url, for example:

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

It still shows the correct Index / Image page and shows a list of all my images. This is just the "pretty" url of this problem.

An 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] 

Is this htaccess file correct? I just need to make my primary domain behave like an addon domain, and its subdirectories adhere to the redirection rules.

+4
source share
2 answers

Do not use mod_rewrite for this, use virtual hosts for this. I will not explain what is detailed here, you will find all the necessary information at the above link. (Hint. You'll probably need names.)

You probably want to move your stuff from ~/public_html/ to a more general place, like /var/www/ or /srv/www .

Additionally: Do not use .htaccess files during production .

+1
source

I organized it the same way you did. Had the same problem with my primary domain. This .htaccess works for me and even solves an ugly URL page.

 # .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] 
0
source

Source: https://habr.com/ru/post/1312136/


All Articles