【问题标题】:HTTP to HTTPS redirect without showing index.phpHTTP 到 HTTPS 重定向而不显示 index.php
【发布时间】:2015-08-14 03:00:07
【问题描述】:

我的网站是 HTTPS“https://website.com”,所以我添加了一个 htaccess 文件,以便在我通过 HTTP 访问网站时重定向到 https。

这是我从 SO 那里得到的代码。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

当我访问http://website.com 时,它会重定向到https://www.website.com,这是正确的。但是当我访问http://www.website.com时,它会重定向到https://www.website.com/index.php

网址中有一个我不喜欢的index.php。有什么可能的解决方案吗?

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    你的代码是这样的:

    RewriteEngine On
    
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    您不需要多个RewriteEngine On 行并在内部重写规则之前保留重定向规则,如图所示。

    确保在测试前清除浏览器缓存。

    【讨论】: