My entire application should be accessed over HTTPS, The vhost is correctly configured and when I call any of my application pages with the HTTPS prefix, it responds correctly.
However when I try to access a page through a generated link within my application. it always points to a regular HTTP link.
I first tried to redirect all traffic to HTTPS using .htaccess file. Spent a few hours trying every redirect method I could find on the net but I always ended up having a infinite redirect loop for some reason.
Now I'm trying to approach this in a different way: generating directly my links in HTTPS. All my links are generated using the URL()
function provided by twig-bridge.
It is pretty easy to configure with Symfony
But I couldnt figure out how to do it with Silex. Is there an easy way to change the routing scheme in Silex to force every link to be generated with an HTTPS prefix ?
Adding requireHttps()
to your routes generator, would do the trick.
Example:
$app->get('/hello-world', function($app) {
return 'Hello world';
})
->bind('hello') // Set route name (optional of course)
->requireHttps(); // This would force your url with https://
You can look at the API as well for more information. You will find everything you need there.
Set environment variable HTTPS
to on
.
Nginx config inside location: fastcgi_param HTTPS on;
Apache's .htaccess or vhost config: SetEnv HTTPS on
Note that default nginx config in official docs sets this variable to off
which may be copypasted to https sites and it's wrong:
http://silex.sensiolabs.org/doc/web_servers.html
Ctrl+F - fastcgi_param HTTPS off;
In case your using the Controller Providers, you can use the requireHttps()
method from Controller
class to require HTTPS for the entire scope of routes:
namespace Acme;
use Silex\Application;
use Silex\Api\ControllerProviderInterface;
class HelloControllerProvider implements ControllerProviderInterface
{
public function connect(Application $app)
{
$controllers = $app['controllers_factory'];
$controllers->get('/', function (Application $app) {
return $app->redirect('/hello');
});
$controllers->get('/another-page', 'Acme\HelloControllerProvider::doSomething')->bind('another-page');
// here it goes
$controllers->requireHttps();
// or even make it more flexible with a config variable
// if ($app['config']['require_https']) (
// $controllers->requireHttps();
// }
return $controllers;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With