【问题标题】:htaccess - redirect to https not workinghtaccess - 重定向到 https 不起作用
【发布时间】:2013-11-23 10:28:52
【问题描述】:

每个人:https://exp-resso.com/blog/post/2011/08/securing-your-expressionengine-website-with-https

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond $1 ^(member|account|checkout|system) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

这会告诉您的服务器“如果 HTTPS 已关闭,并且请求以 会员或帐户或结帐或系统(不区分大小写),重定向 到https://current-domain/current-page”。这是一个很好的简单方法 锁定整个子文件夹/模板组。

我已将其添加到我的 htaccess 文件中,如下所示:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond $1 ^(sign-in|sign-up) [NC]
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
</IfModule>

但是,当我转到我的 http://mydomain.com/sign-in 时,URL 并没有更改为 https://mydomain.com/sign-in。知道有什么问题吗?

编辑 1:

我的 htaccess 也有以下内容(删除“www”),我想知道这两者是否会导致问题?

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

编辑 2:

排除过程,原来是这个问题造成的:

<IfModule mod_rewrite.c>
        RewriteEngine On

        # Removes index.php from ExpressionEngine URLs
        RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

当我注释掉 RewriteRule 时,https:// 是强制的。 是什么导致了冲突?

【问题讨论】:

  • 您为什么使用.htaccess 样式文件而不是正常配置?这些文件容易出错,会减慢一切,并且难以调试。

标签: apache .htaccess


【解决方案1】:

尝试将 (sign-in|sign-up) 条件放入 RewriteRule:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(sign-in|sign-up)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R=301] 

【讨论】:

  • @StackOverflowNewbie 你确定,mod_rewrite 开启了吗?任何重定向都有效吗?
  • @StackOverflowNewbie 我在这里测试了它:htaccess.madewithlove.be 并且成功了。
  • 当我使用它时,它可以工作(https 一切): RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R= 301]跨度>
  • @StackOverflowNewbie 此重定向必须仅适用于 2 个页面:/sign-in 和 /sign-up。不行吗?
  • @StackOverflowNewbie 暂时移除www 条件进行纯测试
【解决方案2】:

这个呢? (如果端口 == 80 则重定向)

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} member [OR]
RewriteCond %{REQUEST_URI} account [OR]
RewriteCond %{REQUEST_URI} checkout [OR]
RewriteCond %{REQUEST_URI} system
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

【讨论】:

  • RewriteEngine On 有必要吗?我是否只是将RewriteCond $1 ^(member|account|checkout|system) [NC] 复制到RewriteRule 上方?
【解决方案3】:

首先确保重写在您的服务器上有效并且 htaccess 已被读取(例如,通过在每个 URL 上发出重定向)。

然后用RewriteCond %{REQUEST_URI} ^/(sign-in|sign-up)代替RewriteCond $1 ^(sign-in|sign-up) [NC]

它工作起来也更容易阅读

所以你的 htaccess 应该是这样的

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/(sign-in|sign-up) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-14
    • 2017-06-21
    • 2018-09-08
    • 2016-04-12
    • 1970-01-01
    • 2016-05-24
    • 2023-03-03
    • 2013-09-08
    相关资源
    最近更新 更多