0

I am trying to redirect my site to always open in HTTPS. I am using CloudFlare and they have a setting to "Always use 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. And if that page also loads in HTTPS then under iFrame any website whose URL hasn't been mentioned with HTTPS doesn't open. Therefore, for that particular page I want to keep the website to be opened under HTTP.

Things I am doing:

  1. In CloudFlare Crypto settings "Always Use HTTPS" is ON.

  2. Then in my page where I want it to opened under HTTP say surf.php

I am using the following PHP code:

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. What should be the best method here?

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
Apple Bux
  • 85
  • 1
  • 9
  • I don't really know anything about cloudflare but maybe you should just use .htaccess for better control over the redirects. https://stackoverflow.com/questions/5818146/how-to-force-rewrite-to-https-except-for-a-few-pages-in-apache – But those new buttons though.. Dec 22 '18 at 08:44
  • But for surf.php it says `The page isn’t redirecting properly` and page doesn't open.@ArtisticPhoenix – Apple Bux Dec 22 '18 at 08:50

1 Answers1

0

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. 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)

So I would first make sure your not forcing HTTPS in your .htaccess file. If so you can add a RewriteCond to exclude your 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. So with this in place your PHP code could do it's job, but you can also do this in htaccess alone. Because you will have dependence on the URL in there anyway, I don't see an issue doing it all in the .htaccess file.

This would basically be the opposite of the above except you know the 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.

I am pretty sure with %{REQUEST_URI} you only have to check if it starts with your URL (minus the host and protocal).

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. But the general idea is sound.

Anyway hope it helps.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • No. I guess its you who actually understood my question backwards. I want my whole site to be redirected in HTTPS except one single page `surf.php`. Did you actually read the whole question? This code is placed in surf.php as I want it to open user HTTP and not HTTPS. Now check your logic again. – Apple Bux Dec 22 '18 at 08:59
  • Fixed, same principal. – ArtisticPhoenix Dec 22 '18 at 09:08
  • If I remove that PHP redirect code then is this all I need in the .htaccess? – Apple Bux Dec 22 '18 at 09:14
  • No, you have to do the opposite for just `/surf.php$` – ArtisticPhoenix Dec 22 '18 at 09:15
  • I am not good with this .htaccess thing. Please update your code so that I can just copy and paste it. Please. – Apple Bux Dec 22 '18 at 09:19
  • That created 500 Internal Server Error! – Apple Bux Dec 22 '18 at 09:27
  • This assumes that `surf.php` is `www.example.com/surf.php` so you will have to modify it with that (for example if its `www.example.com/dir/surf.php` etc). A 500 server error is generally (but not always) a programing error on that page. – ArtisticPhoenix Dec 22 '18 at 09:29
  • Don't forget to "remove" or at least comment out the PHP redirect. This is better handled in HTACCESS because there are no files to include, you can't forget to include the redirect in other words. One easy way to test the 500 error is to comment out the code in that page, and do a simple `echo "hello world";` or something you know will work and see if it does. If it works then you have a error in that code (which I would suggest posting as a separate question) – ArtisticPhoenix Dec 22 '18 at 09:32
  • It is `www.example.com/surf.php`. Now what should be the updated .htaccess code here? A code that forces whole site to load with HTTPS and also forces surf.php to load with HTTP. I am getting confused. – Apple Bux Dec 22 '18 at 13:33
  • I made the assumption that you already had rewrite rules that made the whole site HTTPS. So you just need to exclude that page from that, and then do the reverse for only that page. HTACCESS is really hard to test (without any of the files) and so not something that can really be done in ones head with limited information on a remote system... And as I said I don't do much with it now. I try to avoid it in fact. I had a bad experience moving an ION Cube PHP site to a Windows server. Basically encrypted PHP file, with baked in rules on a server without mod_rewrite – ArtisticPhoenix Dec 22 '18 at 13:47
  • For reference https://stackoverflow.com/questions/14008003/how-to-exclude-a-specific-file-from-a-rewriterule (how to exclude a file from a rewrite rule) – ArtisticPhoenix Dec 22 '18 at 13:51