htaccess - 重定向到https无效

时间:2013-11-23 10:28:52

标签: apache .htaccess

Per: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时,网址不会更改为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://就是强制。 是什么导致了冲突?

3 个答案:

答案 0 :(得分:1)

尝试在RewriteRule中放入(登录|注册)条件:

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

答案 1 :(得分:0)

这个怎么样? (如果port == 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]

答案 2 :(得分:0)

首先确保重写在您的服务器上运行并且读取了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] 
相关问题