How to set document root to be a subdirectory using .htaccess and not VHost

51,900

Solution 1

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/website_1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/website_1/$1 -d [OR]
RewriteCond %{DOCUMENT_ROOT}/website_1/$1 -l
RewriteRule (?!^website_1/)^(.*)$ /website_1/$1 [R=302,L,NC]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Solution 2

Make a entry in a .htaccess. Suppose you have a website abc.com. You want to run another website in directory. Then create a .htaccess in parent directory pointed to abc.com

RewriteEngine on

RewriteCond %{HTTP_HOST} ^abc.com$ [NC,OR]

RewriteCond %{HTTP_HOST} ^www.abc.com$ 

RewriteCond %{REQUEST_URI} !subdirectory/

RewriteRule (.*) /subdirectory/$1 [L]

Solution 3

Rewriting all image requests to root could be a solution. You may try this in one .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# Check if request is for an image at root. Add/remove file types if necessary.
RewriteCond %{REQUEST_URI}  ^/([^.]+)\.(png|jpg|gif|css) [NC]
RewriteCond %{REQUEST_URI}  !/website_1  [NC]
# Rewrite request to point to "website_1" directory
RewriteRule .*               /website_1/%1.%2          [L,NC]
Share:
51,900
GGio
Author by

GGio

Updated on August 24, 2020

Comments

  • GGio
    GGio almost 4 years

    On my local machine the following works perfect:

     <VirtualHost *:80>
           ##ServerAdmin [email protected]
           DocumentRoot "C:/xampp/htdocs/website_1"
           ServerName testpage.com/website_1
           ##ServerAlias www.recruitement.localhost
           ##ErrorLog "logs/dummy-host2.localhost-error.log"
           ##CustomLog "logs/dummy-host2.localhost-access.log" combined
     </VirtualHost>
    

    Howerver Im hosting my website to hosting company called justhost.com and they do not allow me to modify httpd-vhosts.conf or httpd.conf. Now my entire site is coded so that files under website_1 reference to other files under website_1 using simple slash "/" meaning website_1 is treated as document root. This works perfectly on local machine but when uploaded to host it gives me server errors because cant find the files since its trying to locate those files in public_html

    For example:

      public_html
          - website_1
             - script.php
             - style.css
    

    inside my script.php:

    <a href="/style.css">See My Style</a>
    

    this works good on local machine but on host it fails since it tries to locate style.css under public_html and not public_html/website_1

    Is there a way to have multiple document roots without using VHosts? Like using .htaccess or something else. Please I want to try to avoid rewriting the code as much as possible since its around 10 thousands lines of code.

  • GGio
    GGio about 11 years
    I dont have access to httpd.conf since its hosting company not dedicated server.
  • GGio
    GGio about 11 years
    do i need to replace DOCUMENT_ROOT with something?
  • GGio
    GGio about 11 years
    I get redirect loop. Where do I put the .htaccess under public_html or under public_html/website_1?
  • anubhava
    anubhava about 11 years
    As I already mentioned above code will go under DOCUMENT_ROOT/.htaccess. About redirect look may I know what URI did you try?
  • GGio
    GGio about 11 years
    well image is an example it might be different files. All files under website_1 has to have document root at /website_1
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    Yes, it will map all images files regardless of their name as long as the extension is in the condition. No, /website_1 is not the root directory at justhost.com. It is public_html according to your question and I assume /website_1 is a directory under it. But, have you tried the code in the answer and if so, what's the error or problem?
  • GGio
    GGio about 11 years
    Image was just an example. If i have a index.php and style.css under public_html/website_1/ I need to reference to style.css from index.php using just /style.css and not /website_1/style.css. The above code gives me 500 internal server error
  • GGio
    GGio about 11 years
    it adds this code to htaccess instead of your first line of code: # For security reasons, Option followsymlinks cannot be overridden. #Options +FollowSymlinks -MultiViews Options +SymLinksIfOwnerMatch -MultiViews
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    Modified my answer adding the .css file type and one condition to prevent loops (500 error).
  • GGio
    GGio about 11 years
    can you make it so if it was requested any file under website_1 it loads them from website_1/ ? not only images and css it could be php, js etc
  • GGio
    GGio about 11 years
    it gives me 500 internal server error. nothing in error_logs it says this: The server encountered an internal error or misconfiguration and was unable to complete your request.
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    Any file in other words, which means redirecting the whole root to /website_1 or are there exceptions?
  • GGio
    GGio about 11 years
    exceptions are that there might be files under public_html that reference files under public_html those files should load other files from public_html. Basically only the files that are under website_1 should have document root website_1 and not public_html
  • Mauricio Zárate
    Mauricio Zárate over 3 years
    That works like a charm for Ignition-Go (CodeIgniter project)