2017-06-29 216 views
0

這個問題已經被問了很多次,但我已經嘗試了很多,但沒有成功不同的變化 - 在這裏是什麼,我已經試過例子,我所得到的:如何將index.php重定向到HTTPS?

Gives 500 Internal Server Error: 
<Location /index.php> 
RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 
</Location> 

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

Gives TOO MANY REDIRECTS 
RewriteEngine on 
RewriteCond %{HTTPS} off 
RewriteRule ^index\.php$ https://foo.bar [L,R=301] 

Gives TOO MANY REDIRECTS 
<?php 
if($_SERVER["HTTPS"] != "on") 
{ 
    header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); 
    exit(); 
} 
?> 

我一直沒能夠使用任何這些方法獲得HTTPS重定向工作。

我試圖迫使僅index.php來重定向到HTTPS,只有當用戶訪問

http://foo.bar(包括斜線)

http://foo.bar/index.php

+0

[htaccess HTTPS only no redirect]的可能重複(https://stackoverflow.com/questions/29949692/htaccess-https-only-no-redirect) – ctwheels

+2

如何管理SSL?你有直接在你的服務器上安裝的證書,還是你正在使用某種前端代理?你的最後兩個結果意味着後面的結果。 – MrWhite

回答

0

這完美的作品對我來說:

 

    RewriteEngine On 

    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L] 
 
+1

這仍然給出了很多REDIRECTS – unicornication32232

+0

index.php是否有任何正在做重定向的代碼?這聽起來像是有一個重定向循環問題。 –

0

我會建議在每個文件的頂部實現這個PHP代碼:

if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off"){ 
    $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
    header('HTTP/1.1 301 Moved Permanently'); 
    header('Location: ' . $redirect); 
    exit(); 
} 

的HTTP響應狀態代碼301永久移動用於 永久URL重定向,這意味着當前的鏈接或使用接收到響應,應該將更新 URL記錄。應在響應中包含的位置字段中提供新的URL 。 301重定向被認爲是將用戶 從HTTP升級到HTTPS的最佳實踐。

0

500內部服務器是由於您在htaccess中使用的位置指令。位置指令僅用於服務器配置文件。 要將index.php重定向到https,可以使用以下規則。我假設你在apache 2.4或更高版本上。測試該規則之前

RewriteEngine on 
RewriteCond %{REQUEST_SCHEME} http$ 
RewriteRule ^index\.php$ https://example.com [L,R] 

清除瀏覽器緩存。

對於較低版本的Apache,你可以用下面的一個替代條件行:

RewriteCond %{HTTPS} off 

RewriteCond %{SERVER_PORT} 80 
0

您可以使用:

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(index\.php)?$ https://%{HTTP_HOST} [R=301,NC] 

它適用於: http://foo.bar - >https://foo.bar
http://foo.bar/index.php - >https://foo.bar
但不包含http://foo.bar/other

相關問題