-1

I cannot seem to find an answer to this. I have given a thorough search. I have WAMP set up locally and to access it, I type the URL like so:

my-site/

Then when I push the files to Heroku, I access the site like:

my-site.herokuapp.com

I need to force pages to load on https in Heroku. The htaccess for this works fine on Heroku, but breaks my local WAMP environment.

I've been trying to set two Rewrite Conditions and chain them together so that the htaccess only applies on Heroku and is ignored locally. Here is what my htaccess looks like:

RewriteEngine On

##Force SSL 
#only if herokuappis found in the url
RewriteCond %{HTTP_HOST} ^herokuapp$

#Heroku way
RewriteCond %{HTTP:X-Forwarded-Proto} !https

#If neither above conditions are met, redirect to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I borrowed the code from here. How to redirect to HTTPS with .htaccess on Heroku Cedar stack

and have been trying to modify it to work for me, but to no avail.

I've even tried other variations to detect heroku in the URL such as:

RewriteCond %{QUERY_STRING} heroku

or

RewriteCond %{REQUEST_URI} heroku

But I may be misunderstanding how those are used. I am just trying to match two conditions, but honestly, if I could do it with one that would be fine too. If I could simply force HTTPS any time the URL has heroku in it, that would also work.

Thanks in advance.

EDIT: When I load the site on local I get

This site can’t be reached my-site refused to connect.
Try:Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
garek007
  • 395
  • 4
  • 15
  • 1
    `^` means start of string, `$` means end of string. `my-site.herokuapp.com` will never match `^herokuapp$`. – ceejayoz May 21 '19 at 17:56
  • Also, "If neither above conditions are met, redirect to https" is inaccurate. It'll redirect if **both** conditions are met, not neither. – ceejayoz May 21 '19 at 17:56
  • Also also: `heroku` isn't in `QUERY_STRING` nor `REQUEST_URI`. It looks like you're flailing around trying random things instead of understanding what you're doing. I've pointed out some issues in your approach, but **how** does this `.htaccess` break your local? What happens, and what errors do you get? – ceejayoz May 21 '19 at 17:57
  • @ceejayoz edited post – garek007 May 21 '19 at 18:31
  • @ceejayoz and yes, I AM flailing about. I have no idea how to read htaccess directives which is why I posted here. – garek007 May 21 '19 at 18:33
  • Also @ceejayoz I didn't say I wanted it to fire if neither condition was met, I DO want it to fire if BOTH conditions are met, but that is not happening – garek007 May 21 '19 at 18:55
  • As I said in the first comment, your first `RewriteCond` is flawed. You likely want something like `RewriteCond %{HTTP_HOST} \.herokuapp\.com$` instead. – ceejayoz May 21 '19 at 19:03
  • @ceejayoz that appears to have worked. Please post as an answer and I'll mark it. – garek007 May 21 '19 at 19:14
  • And for the record, I tried some variation of that before, but it was my-site\.herokuapp\.com and it did not work. So I guess I don't understand how to up properly – garek007 May 21 '19 at 19:15

1 Answers1

0
RewriteCond %{HTTP_HOST} ^herokuapp$

In a regex, ^ indicates "the start of the string", and $ indicates "the end of the string".

As such, your rule will only apply if the HTTP_HOST is exactly herokuapp. my-site.herokuapp.com will never match this rule.

RewriteCond %{HTTP_HOST} \.herokuapp\.com$

applies the rule to hostnames that end in .herokuapp.com.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368