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

笑着哭i 提交于 2019-11-27 22:30:54

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.

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]

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]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!