Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

screenager's avatar

Request::isSecure() returns false on domain proxied by Cloudflare

Guys,

I have a Laravel 5.3 with a domain name enforced by https:// All links are going over https, all seems fine, except for 2 use cases:

  • Making a ajax request with jQuery, $.post($url), will be sent over http, even if the $url is prefixed by https

  • I also notice that Request::fullUrl() will return the current URL in http

Request::isSecure() returns false.

I have a Heroku PHP 7 hosting, free dyno, running Apache, and my domain name is proxied with a free Cloudflare account.

Here is what I tried:

  • base href in my html is set to my domain name with https.

  • In AppServiceProvider, I do URL::forceSchema("https")

  • I even whitelist the https://www.cloudflare.com/ips-v4 by calling setTrustedProxies in a middleware

  • In my site settings on Cloudflare, SSL in the Crypto tab is on Full, SSL certificates are in place (Universal), and Automatic HTTPS Rewrites is ON. I also made a Page Rule to always use www.

  • Installing a TLS certificate in Cloudflare is not possible, as I'm on the free dyno from Heroku, where I'm not allowed to point to these.

  • In my heroku-apache.conf, I added a rewrite rule to enforce https

Still, jQuery tries to do the POST request over http, logically resulting in a cross origins bug..

0 likes
3 replies
IcyApril's avatar

Cloudflare should act transparently when it comes to SSL, expect in a few use-cases:

  • You are using Flexible SSL, in Flexible SSL will result in requests between Cloudflare and your origin server being unencrypted
  • Your server is redirecting away from HTTPS to the HTTP site, hence you aren't hitting the HTTPS version of your site

If you're using Flexible SSL

The quickest way to fix this is simply to set Cloudflare's SSL setting to Full mode, then ensure you have a Page Rule set-up to enforce SSL. See Configure CloudFlare and Heroku over HTTPS for details.

If Flexible SSL isn't the issue

Chances are that your web server is redirecting away from the HTTPS version of your site to the HTTP version. This could be as simple as the web server trying to add a missing "/" to the end of the URL, causing it to momentarily redirect to the non-HTTPS version of your site. Ensure all application side redirects (.htaccess, Apache config, PHP routing files) all ensure your site redirects to the HTTPS version.

If you're struggling to find out what's the case, try cURLing the HTTPS version of your endpoint and check if you get a 301 or 302 response back.

screenager's avatar

Indeed, I forgot to mention that I also created the Page Rule to enforce www, but it has no effect. Either way, I'm on Full SSL, not Flexible SSL.

I'm gonna look into the second advice.

screenager's avatar

I fixed my initial problem. My routes file (web.php) was not well configured to handle the ajax request, after adding a language prefix to all routes. So the POST request was well sent, but as the route URL didn't exist it any longer, it somehow then tries to do a GET request to the same URL without https.

Please or to participate in this conversation.