htaccess 将除三个页面之外的所有页面重定向到 https

htaccess redirect all but three pages to https

我知道这听起来像是重复的,但我已经尝试了尽可能多的方法,但我仍然没有成功。所以请不要自动将其标记为重复。

我正在尝试重定向 http://www.domian.com to https://www.domain.com 的所有页面,但以下三个页面除外:/products/product1、/products/product2、/products/product3

我还希望这些页面始终指向 http,因为如果 https 页面 link 通过亲戚 link 指向它,它自然会在那里有 https。

另外,这是一个表达式引擎站点,我要从 URI 中删除 index.php。

当前的 Htaccess,导致这三个页面仍然是 https,即使我为这些页面转到 Http。

 ### secure .htaccess file
<Files .htaccess>
 order allow,deny
 deny from all
</Files>

### EE 404 page for missing pages
ErrorDocument 404 /index.php/site/404

###Block Access to directories with no index file
Options -Indexes

### Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp|css|js|flv)$">
  ErrorDocument 404 "File Not Found"
</FilesMatch>

### Although highly unlikely, your host may have +FollowSymLinks enabled at the root level, yet disallow its addition in .htaccess; in which case, adding +FollowSymLinks will break your setup (probably a 500 error), so just remove it, and your rules should work fine.
###Options +FollowSymlinks
RewriteEngine On
RewriteBase /

###Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule ^index\.php(.+)  [R=301,L]

### Add the www
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/ [R=permanent,L]


###each option I try I replace in this block as a reference
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/products/product3
RewriteCond %{REQUEST_URI} !^/products/product1
RewriteCond %{REQUEST_URI} !^/products/product2
RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R=301,L]

######


RewriteCond  !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]
###

也试过这个,结果和上面一样:

RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} !^(domain\.com/products/product1|domain\.com/products/product2|domain\.com/products/product2) [NC]
RewriteRule ^(.*)$ https://www.domain.com/ [R,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(domain\.com/products/product1|domain\.com/products/product2|domain\.com/products/product2) [NC]
RewriteRule ^(.*)$ http://www.domain.com/ [R,L]

单独尝试这个只是为了看看是否能够导航到 http://www.domain.com/products/product1 并且至少不会重写它,但它仍然重写为 https

RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !/products/product1 [NC,OR]
RewriteCond %{REQUEST_URI} !/products/product2 [NC,OR]
RewriteCond %{REQUEST_URI} !/products/product3 [NC,OR]
RewriteRule ^(.*)$ https://www.domain.com/ [R,L]

也试过了,它仍然将这些页面写入 https。另外,如果我尝试转到 http ,它会重写为 https 并且不会删除 index.php

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/products/product3
RewriteCond %{REQUEST_URI} !^/products/product1
RewriteCond %{REQUEST_URI} !^/products/product2
RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R=301,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^/products/product3 http://www.domain.com/products/product3 [R=301,QSA,L,NE]
RewriteRule ^/products/product1 http://www.domain.com/products/product1 [R=301,QSA,L,NE]
RewriteRule ^/products/product2 http://www.domain.com/products/product2 [R=301,QSA,L,NE]

老实说,我已经尝试了大约 10 -15 个以上的版本,但仍然无法得到它。包括添加index.php?到 request_URI 以防它在删除之前将其视为 request_URI 的一部分。我要么最终页面仍然重写为 https,要么重定向循环土地。

我是不是遗漏了一些简单的东西,是不是顺序错了?我只是不知所措。 感谢您的帮助。

更新: 这是我用来让它工作的方法:

RewriteCond %{HTTPS} on    
RewriteCond %{THE_REQUEST} (\s/products/product1|\s/products/product2|\s/products/product3) [NC]    
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]    


RewriteCond %{HTTPS} off    
RewriteCond %{THE_REQUEST} !\s/products/product1 [NC]    
RewriteCond %{THE_REQUEST} !\s/products/product2 [NC]    
RewriteCond %{THE_REQUEST} !\s/products/product3 [NC]   
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

以下是我们如何在一个带有网络摄像头的网站上实现这一点,该网络摄像头位于无法在 SSL 上运行的 iframe 中。这也是一个 ExpressionEngine 站点。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#force www instead of non-www
RewriteCond %{HTTP_HOST} ^domain.co.uk
RewriteRule ^(.*)$ http://www.domain.co.uk/ [R=301,L] 

# HTTP/HTTPS handling 
# Force HTTP for webcam page only
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/webcam-page [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

# Force HTTPS for all pages except webcam
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/webcam-page [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]  

# Removes index.php from ExpressionEngine URLs
RewriteCond  !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/ [L]
</IfModule>