2015-09-15 103 views
5

我想通過htaccess強制https並得到「太多重定向」錯誤。以下是我正在使用的努力來實現這一點。重定向到HTTPS返回錯誤「太多重定向」

RewriteEngine On 
RewriteCond %{HTTPS} !=on 
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 

任何想法,我如何能做到這一點或爲什麼這似乎並沒有工作?

編輯:我跟着從這裏回答Force https://www. for Codeigniter in htaccess with mod_rewrite這似乎是導致我的重定向循環。當我刪除這部分:

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

重定向循環消失;但它不會重定向到https。

+0

[強制HTTPS的可能重複:// WWW。爲Codeigniter在與mod \ _rewrite htaccess](http://stackoverflow.com/questions/8503663/force-https-www-for-codeigniter-in-htaccess-with-mod-rewrite) – RiggsFolly

+0

我真的遇到上面的帖子在發佈之前。我嘗試使用該帖子中的建議,這似乎是導致重定向循環。 – Mike

回答

1

正如RiggsFolly提到的,用語句的問題,但如果不工作,你可以嘗試迫使它在頭標籤:

$use_sts = true; 

// iis sets HTTPS to 'off' for non-SSL requests 
if ($use_sts && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { 
    header('Strict-Transport-Security: max-age=31536000'); 
} elseif ($use_sts) { 
    header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], true, 301); 
    // we are in cleartext at the moment, prevent further execution and output 
    die(); 
}`` 
+0

如果我不能在htaccess中使用它,我會給這個鏡頭。謝謝。 – Mike

14

我不能肯定地說,如果這是相關到一個(或某些)託管服務提供商,但沒有任何解決方案爲我工作。由於託管服務提供商的限制,我無法獲得apache版本,除非它是2.x.

這是什麼做的,也許有幫助的人:

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