Why is Heroku trimming trailing slashes?

2019-09-11 01:33发布

问题:

I have a Laravel webapp that I am developing for a Heroku environment. On the heroku environment, if I visit a url with a trailing slash (I.E. http://example.com/) I get a 301 redirect removing the trailing slash (I.E. http://example.com). This would be fine behavior, except that the rewrite is moving from https to http!

I've spent some time trying to reproduce the issue locally in my development environment and I cannot, which has led me to believe that the issue is with Heroku's configuration of Apache, but uploading a custom .htaccess turning RewriteEngine Off didn't stop the rewrite either.

Has anyone encountered a similar issue?

My procfile just containsweb: vendor/bin/heroku-php-apache2 public so it's quite minimal.

回答1:

Heroku does not perform such redirects. Apache adds slashes for directory indexes if enabled, but that's it.

Your application is doing that redirect somewhere, either in code or a .htaccess, and the request protocol is not preserved because the request to the dyno was HTTP - termination of SSL happens at the load balancer.

Set up your application to trust the Heroku load balancer as a proxy so it reads the request protocol from X-Forwarded-Proto, or change your rewrite rules to do just that.