简体   繁体   English

PHP https网址转发

[英]PHP https url forwarding

Ok so I need my site to automatticly forward all requests to the server for http to https. 好的,所以我需要我的站点将所有请求自动转发到服务器,以将HTTP发送到https。 I was using php with no real problems but now godaddy is telling me that I need to change the way i am doing this. 我使用的是php,没有任何实际问题,但是现在Godaddy告诉我,我需要更改执行此操作的方式。 Is my code correct, should i change how im doing this, what method is better? 我的代码是否正确,我应该更改即时消息的执行方式,哪种方法更好? Thanks in advance for the help! 先谢谢您的帮助!

<?php
if ($_SERVER['SERVER_PORT']!=443)
{
$url = "https://". $_SERVER['SERVER_NAME'] . ":443".$_SERVER['REQUEST_URI'];
header("Location: $url");
}
?>

You don't need to include the port number... https' default port is 443 already. 您不需要包括端口号... https的默认端口已经是443。 You'd only need an explicit port number if you're running an https service on some OTHER port. 仅在其他端口上运行https服务时,才需要一个明确的端口号。 As well, why the duplicate redirect? 同样,为什么重复重定向? You've got 2 if() blocks doing the exact same thing. 您有2个if()块执行完全相同的操作。

However, just testing for port 443 is not sufficient. 但是,仅测试端口443是不够的。 You can run ANY service you want on any port. 您可以在任何端口上运行所需的任何服务。 It's just that HTTPS usually is on 443. But nothing says it HAS to be. 只是HTTPS通常在443上。但是没有人说它必须存在。 There's $_SERVER['HTTPS'] which explicity tells if you if HTTPS is in use or not. 有一个$_SERVER['HTTPS']明确告诉您是否正在使用HTTPS。

A better method would be to use .htaccess and mod_rewrite to do the redirect. 更好的方法是使用.htaccess和mod_rewrite进行重定向。

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

This is explained in more detail here . 这将在此处更详细地说明。

I would use something as described here: 我将使用此处所述的内容:

enter link description here 在此处输入链接说明

I would not like to rely for port 443. Someone can configure normal traffic towards 443 or use another port for https (both rare). 我不想依靠端口443。有人可以配置朝向443的正常流量,或将另一个端口用于https(这两种情况很少见)。

Additionally since it is the default, you can ommit the ":443"... 另外,由于它是默认设置,因此您可以省略“:443” ...

You could use htaccess for doing this: 您可以使用htaccess来做到这一点:

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.example.com/require-secure/ [R]

Hope it helps 希望能帮助到你

I am using this code in my .htaccess file which works great: 我在我的.htaccess文件中使用此代码,效果很好:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.ch/$1 [R,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM