【问题标题】:Forcing https on the whole site but not on one folder在整个站点上强制 https 但不在一个文件夹上
【发布时间】:2015-11-28 10:42:54
【问题描述】:

谁能告诉我如何在我的整个网站上强制使用 https,而不是在单个文件夹或 url 上。 目前我有这个代码:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^thatmysite\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://thatmysite.com/$1 [R,L]

但是,如果我在根 htaccess 中添加第二个代码以从 /thatsite.com/printing 文件夹中删除 https,我会得到一个重定向循环,因为我强制代码 http 到 https 而不是 https 到 http...

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

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

请问您知道解决方法吗?我一直在寻找整个互联网,找不到一个好的答案。

【问题讨论】:

    标签: apache .htaccess mod-rewrite https


    【解决方案1】:

    我通过将此代码放在托管 http 站点的目录的根文件夹(例如 public_html)的 .htaccess 文件中解决了这个问题

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/TheFolderYouCanAccessWithoutHttp/
    RewriteRule (.*) https://yourdomain.xyz/$1 [R=301,L]
    

    【讨论】:

    • 感谢您的回复!
    【解决方案2】:

    在您的 .htaccess 顶部尝试以下 2 条规则:

    RewriteEngine On
    
    RewriteCond %{HTTPS} off
    RewriteCond %{THE_REQUEST} !/printing [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTPS} on
    RewriteCond %{THE_REQUEST} /printing [NC]
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

    • 我印象深刻,这完美!谢谢,我到处找这个。
    猜你喜欢
    • 2015-01-08
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 2023-03-23
    • 2012-09-17
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多