Search code examples
apache.htaccesssslherokumod-rewrite

How to force Heroku PHP app to use SSL certificate (https)?


I am trying to force my heroku web app to use the automatic certificate offered from heroku. I need to force all my links to use https. So I added lines:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

To the htaccess file, but now I get the error "This page isn't working, too many redirects". What do I put in my htaccess file to force https? Everyone online I've found uses this, but if I delete my other rules my website does not load.

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Solution

  • Figured it out. In Laravel under AppServiceProvider.php, you can put this is the boot() function to force https on your laravel app.

    Hope this helps anyone!

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if($this->app->environment('production')) {
            \URL::forceScheme('https');
        }
    }