How to redirect all HTTP requests to HTTPS in Silex?

Asked

Viewed 106 times

1

In Silex, there is some possibility of making a filter that redirects a url to https when it does not have https?

I know how to do it in the Laravel, but I’d like to do the same operation with Silex.

For example, when accessing the url http://www.exemplo.com, I’d like you to redirect to https://www.exemplo.com.

How do I do it in Silex?

1 answer

2


As stated in SOEN I guess that’s how it is:

$app = new Silex\Application();

$app['controllers']->requireHttps();

$app->get('/', function () use ($app) {
    return 'Olá mundo!';
});

$app->run();

Documentation in Silex\Controller::requireHttps

Note:

Wallace left a hint, if you are in HTTPS development environment you may not need it, then you can detect if the script is in debug mode, like this:

$app = new Silex\Application();

if (!$app['debug']) {
   $app['controllers']->requireHttps();
}

Or to make it simple:

$app = new Silex\Application();

$app['debug'] || $app['controllers']->requireHttps();

You can also set this by .htaccess as I explained in:

Example:

# Redireciona para o HTTPS independente do domínio
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Adiciona www. no prefixo do domínio
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  • The idea is cool, pity that has to define in route by route :\

  • @Wallacemaxters this one requireHttps is "extended", I believe you can define in the before

  • I tried to use in the before, didn’t work out :\

  • Try calling it that in the before: $app['controllers']->requireHttps();, didn’t work out so well :\

  • @Wallacemaxters I’m installing and I’ll rephrase the answer

  • I sent you a message on the website, I found out what it’s like... Add it there, because I don’t want to answer :p

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.