使用HTTPS在.htaccess中自动加载网站到子文件夹

问题描述:

我刚刚在服务器上安装了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文件? –

您可以使用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方法发布答案,或者直接更新我的答案。 –