简体   繁体   English

重定向站点以使用HTTPS

[英]Redirecting site for HTTPS

I am trying to redirect my site to always open in HTTPS. 我试图将我的站点重定向为始终在HTTPS中打开。 I am using CloudFlare and they have a setting to "Always use HTTPS". 我正在使用CloudFlare,它们的设置为“始终使用HTTPS”。 But there is a page on my website where I do not want to use HTTPS as it opens other websites under an iFrame. 但是我的网站上有一个页面,我不想使用HTTPS,因为它会在iFrame下打开其他网站。 And if that page also loads in HTTPS then under iFrame any website whose URL hasn't been mentioned with HTTPS doesn't open. 如果该页面也加载到HTTPS中,则在iFrame下不会打开未使用HTTPS提及其URL的网站。 Therefore, for that particular page I want to keep the website to be opened under HTTP. 因此,对于该特定页面,我想保持网站在HTTP下打开。

Things I am doing: 我正在做的事情:

  1. In CloudFlare Crypto settings "Always Use HTTPS" is ON. 在CloudFlare加密设置中,“始终使用HTTPS”处于启用状态。

  2. Then in my page where I want it to opened under HTTP say surf.php 然后在我要在HTTP下打开的页面中说surf.php

I am using the following PHP code: 我正在使用以下PHP代码:

if($_SERVER['HTTP_HOST'] != 'localhost'){
  if(isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'on'){
    if(!headers_sent()){
      header("Status: 301 Moved Permanently");
      header(sprintf('Location: http://%s%s',$_SERVER['HTTP_HOST'],$_SERVER['REQUEST_URI']));
      exit();
    }
  }
}

Now the page doesn't open and says "The page isn't redirecting properly". 现在,该页面无法打开,并显示“页面无法正确重定向”。 What should I do? 我该怎么办? Is there any other method to accomplish this? 还有其他方法可以做到这一点吗? I want to use HTTPS in whole website so "Always use HTTPS" settings in cloudflare should be ON except just surf.php. 我想在整个网站中使用HTTPS,因此,除了surf.php之外,cloudflare中的“始终使用HTTPS”设置应该为ON。 What should be the best method here? 这里最好的方法是什么?

It sounds like you are in a redirect loop. 听起来您处于重定向循环中。 Where you have a .htaccess file that forces HTTPS, and then you redirect to HTTP using PHP. 在其中具有强制HTTPS的.htaccess文件的位置,然后使用PHP重定向到HTTP。 Then that new request has all the same rules applied to it so that it gets redirected by .htaccess again to HTTPS, and so on (to infinity) 然后,该新请求将所有相同的规则应用到该请求,以便通过.htaccess再次将其重定向到HTTPS,依此类推(无限远)

So I would first make sure your not forcing HTTPS in your .htaccess file. 因此,我首先要确保您不要在.htaccess文件中强制使用HTTPS。 If so you can add a RewriteCond to exclude your URL: 如果是这样,您可以添加RewriteCond以排除您的URL:

#RewriteEngine On  #-- if not included elsewhere

#if HTTPS is not on (then continue)
RewriteCond %{HTTPS} !=on

#add this rule in  (if not our page, then redirect to HTTPS)
RewriteCond %{REQUEST_URI} !^/surf\.php$

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

When mod rewrite hits a Rewrite condition if it fails (is false) it will disregard the next rewrite rule. 如果mod rewrite如果失败(错误)达到Rewrite条件,它将忽略下一个重写规则。 So with this in place your PHP code could do it's job, but you can also do this in htaccess alone. 因此,有了这个适当的位置,您的PHP代码就可以完成它的工作,但是您也可以仅在htaccess中做到这一点。 Because you will have dependence on the URL in there anyway, I don't see an issue doing it all in the .htaccess file. 因为无论如何您都将依赖那里的URL,所以在.htaccess文件中我看不到这样做的问题。

This would basically be the opposite of the above except you know the url. 基本上,这与上面的相反,只是您知道url。 Something like this: 像这样:

#if HTTPS is not on (then continue)
RewriteCond %{HTTPS} !=on
#add this rule in  (if not our page, then redirect to HTTPS)
RewriteCond %{REQUEST_URI} !^/surf\.php$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#if HTTPS is not off (then continue)
RewriteCond %{HTTPS}!=off
#  (if is our page, then redirect to HTTP)
RewriteCond %{REQUEST_URI} ^/surf\.php$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I can't really test this though, but that's the general idea. 虽然我不能真正测试一下,但这是一般的想法。 If HTTPS is no off, and the %{REQUEST_URI} is our page !^/surf.php$ redirect to HTTP... Basically you have to punch a hole through the HTTPS rule and then force http. 如果没有关闭HTTPS,并且%{REQUEST_URI}是我们的页面!^/surf.php$重定向到HTTP ...基本上,您必须在HTTPS规则上打一个漏洞,然后强制使用http。

I am pretty sure with %{REQUEST_URI} you only have to check if it starts with your URL (minus the host and protocal). 我非常确定使用%{REQUEST_URI}您只需要检查它是否以您的URL开头(减去主机和协议)即可。

I'll admit I'm a bit rusty with complex HTACCESS rules, spoiled by MVC routers, so this may very well not be 100% correct. 我承认我对被MVC路由器破坏的复杂HTACCESS规则有些不满意,所以这可能不是100%正确的。 But the general idea is sound. 但是总体思路是合理的。

Anyway hope it helps. 无论如何希望它会有所帮助。

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

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