【问题标题】:Forcing Non-SSL when specific url parameter is given给出特定 url 参数时强制使用非 SSL
【发布时间】:2017-01-02 01:58:44
【问题描述】:

好的,我发现我可以通过更改 .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 文件?

【问题讨论】:

    标签: php html .htaccess ssl


    【解决方案1】:

    如果有人遇到同样的问题,此代码对我有用(将每个链接重定向到 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]
    

    【讨论】:

      【解决方案2】:

      这个问题是DUPLICATE

      顺便说一句

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

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

      【讨论】:

      • 对不起,我正在寻找它但没有找到它
      猜你喜欢
      • 2011-02-15
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      相关资源
      最近更新 更多