仅为几个网址强制使用HTTPS?

时间:2017-04-11 20:30:10

标签: apache .htaccess ssl https url-redirection

我已经尝试过每一件事,只有两个特定的网址才能重定向到https。没有工作。这就是我现在在.htaccess

中所拥有的内容
RewriteCond %{REQUEST_FILENAME} topdeal(.*)
RewriteCond %{REQUEST_FILENAME} watchstore(.*)
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

2 个答案:

答案 0 :(得分:0)

使用THE_REQUEST变量代替REQUEST_FILENAME来尝试此规则:

RewriteCond %{THE_REQUEST} watchstore|topdeal [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

答案 1 :(得分:0)

您可以在.htaccess文件中使用以下内容。除了目录watchstoretopdeal之外,下面的操作是将所有内容强制为HTTP。两者都被迫使用HTTP。

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(watchstore|topdeal)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(watchstore|topdeal)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

确保在测试之前清除缓存。

修改

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^\/(watchstore|topdeal)
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^\/(watchstore|topdeal)
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]