How to set document root to be a subdirectory usin

2020-01-31 11:08发布

On my local machine the following works perfect:

 <VirtualHost *:80>
       ##ServerAdmin postmaster@dummy-host2.localhost
       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.

3条回答
虎瘦雄心在
2楼-- · 2020-01-31 11:33

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.

查看更多
相关推荐>>
3楼-- · 2020-01-31 11:33

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]
查看更多
我命由我不由天
4楼-- · 2020-01-31 11:47

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]
查看更多
登录 后发表回答