首页 > 解决方案 > http 重定向到 index.php 而不是 https

问题描述

所以我需要将 HTTP 协议重定向到 https,我的 SSL 已经启用并且可以工作,但问题是:每当我输入 HTTP URL 时,我都会被重定向到我网站的 index.php 页面。

这是我的代码:

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

# add trailing slash to directories and force SSL
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !(/$) 
RewriteCond %{SERVER_PORT} 80 
RewriteRule (.*) https://example.com/$1/ [R=301,L]

# And for the files
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

标签: phphttphttpsurl-redirectionhttp-redirect

解决方案


根据 Apache2文档,您可以通过以下方式简化虚拟主机:

   <VirtualHost *:80>
      ServerName www.example.com
      Redirect Permanent/ https://www.example.com/
   </VirtualHost>

   <VirtualHost *:443>
      ServerName www.example.com
       DocumentRoot /usr/local/apache2/htdocs
       SSLEngine On
       //ALL Your specifics configurations here
   </VirtualHost>

这允许您将端口80上的每个请求重定向到https。因此,您只需将自己的目录索引配置到443的虚拟主机中。

希望这有帮助。


推荐阅读