【问题标题】:Auto load the website to Subfolder with HTTPS in .htaccess使用 .htaccess 中的 HTTPS 将网站自动加载到子文件夹
【发布时间】:2018-03-19 07:47:10
【问题描述】:

我刚刚在服务器上安装了 SSL 证书。

当前网址:www.example.com

当前工作的 htaccess 文件:

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

它正在重定向到 public/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。

我怎样才能结合这些规则,以便它将重定向到带有 https url 的 public/index.php。

PS:公共/htaccess

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

请帮忙!

谢谢

【问题讨论】:

  • 是否需要使用.htaccess重定向到HTTPS版本?
  • 如果您有其他方法,请!可能会有所帮助
  • 你在使用框架吗?或者是否有任何 PHP 文件包含在所有其他文件中?
  • 不是我在使用框架。

标签: php .htaccess redirect mod-rewrite https


【解决方案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 文件中,该文件包含在所有其他文件中。

【讨论】:

  • 我将删除 .htaccess 部分我不是使用 .htaccess 的“专家”,我希望其中一个人可以使用 .htaccess 方法发布答案或简单地更新我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
  • 2011-06-13
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多