How to remove a subdirectory from the url when for

2019-03-01 21:15发布

问题:

On a shared server I am forced to manage document roots of domains from cpanel which is buggy and consumes lots of time. So I redirected all domains to one directory lets say root. An example:

sd1.domain.com  ---> public_html/web/
sd2.domain.com  ---> public_html/web/
sd1.domain2.com ---> public_html/web/
sd2.domain2.com ---> public_html/web/

When user browses these websites, I forward this websites using htaccess rules such as

RewriteCond %{HTTP_HOST}   ^sd1.(domain1|domain2).com [NC]
RewriteCond %{REQUEST_URI} !^/sd1/.*
RewriteRule   ^(.*)  sd1/$1  [L]

Now this does forward the request to different folder but afterwards all the url have sd1 attached on the last part

Here is an illustration

sd1.domain.com            ---> sd1.domain.com                 #Works correctly
sd1.domain.com/page1.html ---> sd1.domain.com/sd1/page1.html  # See the word sd1 on the middle

How to remove the folder portion from the url??

回答1:

if I understand your question correctly, you should be able to do it by using the flag PT (Passthru), which causes the rewrite target to be passed back to the URL mapping engine:

RewriteCond %{HTTP_HOST}   ^sd1.(domain1|domain2).com [NC]
RewriteCond %{REQUEST_URI} !^/sd1/.*
RewriteRule   ^(.*)  sd1/$1  [PT,L]

Hope it helps.