Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将index.php重定向到仅HTTPS?_Php_.htaccess_Https - Fatal编程技术网

如何将index.php重定向到仅HTTPS?

如何将index.php重定向到仅HTTPS?,php,.htaccess,https,Php,.htaccess,Https,这个问题已经被问了很多次,但我尝试了很多不同的变体,但都没有成功-以下是我尝试过的和得到的示例: Gives 500 Internal Server Error: <Location /index.php> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} </Location> Gives TOO MANY REDIRECTS Re

这个问题已经被问了很多次,但我尝试了很多不同的变体,但都没有成功-以下是我尝试过的和得到的示例:

Gives 500 Internal Server Error:
<Location /index.php>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</Location>

Gives TOO MANY REDIRECTS
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Gives TOO MANY REDIRECTS
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^index\.php$ https://foo.bar [L,R=301]

Gives TOO MANY REDIRECTS
<?php
if($_SERVER["HTTPS"] != "on")
{
    header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
    exit();
}
?>
给出500个内部服务器错误:
重新启动发动机
重写条件%{HTTPS}关闭
重写规则(*)https://%{HTTP\u HOST}%{REQUEST\u URI}
提供了太多的重定向
重新启动发动机
重写cond%{ENV:HTTPS}!在…上
重写规则^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
提供了太多的重定向
重新启动发动机
重写条件%{HTTPS}关闭
重写规则^index\.php$https://foo.bar [L,R=301]
提供了太多的重定向
我无法使用这些方法中的任何一种使HTTPS重定向正常工作

我试图仅在用户访问时强制index.php重定向到HTTPS

(包括尾随斜杠)


这对我来说非常合适:


    RewriteEngine On

    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

我建议在每个文件的顶部实现以下PHP代码:

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();
}
永久移动的HTTP响应状态代码301用于 永久URL重定向,意味着使用 应更新接收响应的URL。新网址 应在响应中包含的位置字段中提供。 301重定向被认为是升级用户的最佳实践 从HTTP到HTTPS


500内部服务器是由于您在htaccess中使用的Location指令造成的。Location指令仅在服务器配置contaxt中使用。 要将index.php重定向到https,可以使用以下规则。我假设您使用的是apache 2.4版或更高版本

RewriteEngine on
RewriteCond %{REQUEST_SCHEME} http$
RewriteRule ^index\.php$ https://example.com [L,R]
测试此规则之前,请清除浏览器缓存

对于较低版本的apache,可以使用以下选项之一替换条件行:

RewriteCond %{HTTPS} off

RewriteCond %{SERVER_PORT} 80 
您可以使用:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(index\.php)?$ https://%{HTTP_HOST} [R=301,NC]
它与:
http://foo.bar
->
https://foo.bar

http://foo.bar/index.php
->
https://foo.bar


但不适用于
http://foo.bar/other

好的,我的网站Joomla 3.5中的网站也面临同样的问题 我正在使用下面的代码。它在我的Joomla中正常工作!3.5.1.

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.yourdomain.ac.in/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.yourdomain.ac.in/$1 [L,R=301] 

SSL管理方式的可能重复?您是否在服务器上直接安装了证书,或者您正在使用某种前端代理?你的最后两个结果意味着后面的结果。这仍然会给出太多的重定向。Does index.php有任何代码正在执行重定向吗?听起来好像存在重定向循环问题。