使用.htaccess限制https访问除1个文件夹以外的所有内容

时间:2012-02-12 17:38:07

标签: .htaccess mod-rewrite

我希望文件夹https_folder(包括所有子文件夹和文件)强制使用https,但是每个其他目录或文件都使用http。

文件夹结构:

  1. foldera
  2. FolderB中
  3. https_folder
  4. folderc
  5. 我试着按照以下方式设置它,但是我似乎没有让它工作

    重定向http / https_folder

    RewriteCond %{SERVER_PORT} = 80
    RewriteRule ^https_folder/?$ https://%{HTTP_HOST}%/httpd_folder [R=301,QSA,L,NE]
    

    重定向到https非/市场页面

    RewriteCond %{SERVER_PORT} =443
    RewriteCond %{REQUEST_URI} !^/https_folder [NC]
    RewriteRule ^/?(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
    

    任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

这样做:

RewriteCond %{REQUEST_URI} /https_folder/? [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(https_folder)(/.*)?$ https://%{HTTP_HOST}/$1$2 [R=301,L,NE]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/https_folder/? [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L,NE]

答案 1 :(得分:0)

将此代码放入.htaccess文件中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

# redirect to https if URI has https_folder
RewriteCond %{HTTPS} off
RewriteRule ^https_folder/? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

# redirect to https if URI doesn't have https_folder
RewriteCond %{HTTPS} on
RewriteRule ^(?!https_folder/?)(.*)$ http://%{HTTP_HOST}/$1 [R=301,L,NC]