使用htaccess重写域并从URL中删除子文件夹

时间:2014-08-28 14:17:09

标签: php apache .htaccess mod-rewrite redirect

我有多个域,我希望子文件夹使用.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]

但这意味着网址仍然可以包含子文件夹:example.com/subfolder/img/file.png。如何阻止/subfolder被访问?

1 个答案:

答案 0 :(得分:1)

您需要一条新规则从您的网址中删除/subfolder/,如下所示:

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]