2017-10-07 66 views
0

我剛剛在服務器上安裝了SSL證書。使用HTTPS在.htaccess中自動加載網站到子文件夾

當前網址:www.example.com

當前工作htaccess文件:

RewriteEngine on 
RewriteRule ^$ public/ [L] 
RewriteRule ^(.*)$ public/ [L] 

它重定向到公共/ index.php文件。

但問題是當我在htaccess中添加HTTPS代碼,然後這個public/index.php不起作用。

RewriteEngine on 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} ^example\.com$ [OR] 
RewriteCond %{HTTP_HOST} ^www\.example\.com$ 
RewriteRule ^/?(.*) https://www.example.com/$1 [R] 

當我刪除公共重定向部分,然後HTTPS工作,但沒有指向public/index.php。

如何合併這些規則,以便將其重定向到public/index.php與https url。

PS:公共/ htaccess的

ReWriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 

請幫幫忙!

謝謝

+0

是否需要使用.htaccess重定向到HTTPS版本? –

+0

如果您有任何其他方式,請!它可能會幫助 – NDesai

+0

你在使用框架嗎?或者是否有包含在所有其他文件中的任何PHP文件? –

回答

1

您可以使用PHP來重定向到您的HTTPS網站:

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(); 
} 

來源:Redirecting from HTTP to HTTPS with PHP

然而,需要包含在每一頁上這一段代碼,或將其包含在包含在所有其他文件中的PHP文件中。

+0

更新回答無效! – NDesai

+0

我將刪除.htaccess部分我不是使用.htaccess的「專家」,我希望其中一個人可以使用.htaccess方法發佈答案,或者直接更新我的答案。 –

相關問題