简体   繁体   中英

Rewrite domain using htaccess and remove subfolder from URL

I have multiple domains which I want subfolders for using .htaccess :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/subfolder/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /subfolder/$1 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ subfolder/index.html [L]

But this means URLs can still contain the subfolder: example.com/subfolder/img/file.png . How do I stop /subfolder from being accessible?

You need a new rule to remove /subfolder/ from you URLs like this:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ 
RewriteCond %{THE_REQUEST} \s/+subfolder/(\S*)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]

RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ 
RewriteCond %{REQUEST_URI} !^/subfolder/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ subfolder/$1 [L]

RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ 
RewriteRule ^/?$ subfolder/index.html [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM