【问题标题】:redirect to SSL with the exception of one URL重定向到 SSL,但一个 URL 除外
【发布时间】:2017-07-11 16:02:09
【问题描述】:

我经营着一个小型 bootstrap themes 网站,允许用户上传主题和模板。

我已经能够通过我的 .htaccess 文件将任何非 SSL 连接重定向到 https:

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

我现在需要将用户重定向到非 SSL 页面,特别是一页。这是一个包含 iframe 的页面,可从主题作者网站加载预览,而这些网站并不总是通过 SSL 提供服务。

URL 总是在域名前面有“预览”一词。

这是我尝试过的,不幸的是没有成功:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/preview [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} on

RewriteCond %{REQUEST_URI} ^/preview [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]

这里有两个例子:

非预览链接http://www.bootstrapcovers.com/bootstrap-themes/all/free/sort.downloads/page.1

预览链接 http://www.bootstrapcovers.com/preview/1/adminlte-admin-control-panel

知道为什么它不起作用或我在 .htaccess 文件中缺少什么吗?

谢谢, -保罗

【问题讨论】:

    标签: php .htaccess redirect url-redirection


    【解决方案1】:

    使用THE_REQUEST 而不是REQUEST_URI,这可能会因其他模块或规则而改变:

    RewriteEngine On
    
    RewriteCond %{HTTPS} off
    RewriteCond %{THE_REQUEST} !\s/+preview [NC]
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
    
    RewriteCond %{HTTPS} on
    RewriteCond %{THE_REQUEST} \s/+preview [NC]
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
    
    # other rules go here
    

    还要确保清除浏览器缓存。

    【讨论】:

    • 太棒了,成功了! REQUEST_URI 和 THE_REQUEST 有什么区别?
    • THE_REQUEST 是 Apache 从客户端收到的原始 Web 请求,不会因其他规则而改变。
    • 很高兴知道...再次感谢您的帮助。我在这上面花了太多时间:)
    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 2014-11-16
    • 2015-03-13
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多