在给定特定url参数时强制使用非SSL

时间:2016-08-25 14:58:13

标签: php html .htaccess ssl

好的,我发现我可以通过更改.htaccess文件中的内容强制用户使用SSL:

RewriteEngine On
RewriteBase /

# Force SSL
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Deny Access to all .htaccess
<Files .htaccess>
order allow,deny
deny from all
</Files>

我写的.htaccess文件并不是很好,所以我很可能从互联网上复制粘贴它。

我的问题是,是否可以始终强制ssl有一个例外(当有人访问具有特定url参数的http站点时)。

示例1:

http://example.com/test?name=John

被重定向到

https://example.com/test?name=John

示例2:

http://example.com/test?name=John&nossl=1

未被重定向到

https://example.com/test?name=John&nossl=1

是否有可能在htaccess中写入或者我是否需要在每个执行此操作的页面中包含一个php文件?

2 个答案:

答案 0 :(得分:0)

这个问题是DUPLICATE

顺便说一句

将其放在# Force SSL条件之前。

RewriteCond %{QUERY_STRING} (^|&)nossl=(1)(&|$)
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

答案 1 :(得分:0)

如果有人遇到同样的问题,此代码适用于我(将每个链接重定向到ssl /如果nossl = 1,则在需要时重定向到http):

# Force SSL
RewriteCond %{QUERY_STRING} !(^|&)nossl=(1)(&|$)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force Non-SSL if URL parameter nossl=1
RewriteCond %{QUERY_STRING} (^|&)nossl=(1)(&|$)
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]