【问题标题】:force ssl except on home/ index page with htaccess使用 htaccess 在主页/索引页面上强制使用 ssl
【发布时间】:2011-06-27 09:13:35
【问题描述】:

很抱歉,我在正则表达式方面有点头晕,我尝试过但失败了。

你能告诉我如何使索引/主页不在 ssl 中吗?现在我有这个重写规则,它强制整个站点使用 ssl:

# force ssl
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我需要的是主页/索引页面不在 ssl 中。

【问题讨论】:

  • 出于兴趣,你为什么要整个网站加密除了主页?
  • 好吧..实际上我只是希望登录的页面在 ssl 中。不需要用户登录的页面不必使用 ssl。所以这是一个开始。

标签: php regex apache


【解决方案1】:

试试:

<IfModule mod_rewrite.c>
    RewriteEngine on

    # force ssl
    RewriteCond %{REQUEST_URI} !^$  # no ssl on /
    RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /index\.php\ HTTP/ # no ssl on index.php
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

【讨论】:

  • 嘿...这真的行不通。试过了,仍然显示任何 https。
  • @David:此代码执行以下操作:如果请求的页面不是根/index.php,并且HTTPS 未打开,它将重定向到https。否则,它将使您的请求保持原样。如果您的请求是https://www.example.com/index.php,则此代码不会将其重定向到http://www.example.com/index.php。如果你想达到第二个,请告诉我。否则,请告诉我你是如何测试它的,哪些 URL 不起作用?
【解决方案2】:

正确的条件是:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

确保重写引擎已加载。

【讨论】:

  • 但是主页/索引页面呢?
【解决方案3】:

你想要另一个RewriteCond

RewriteCond %{REQUEST_URI} !^$

【讨论】:

    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多