Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force https for prod but http for dev environment?

I have a symfony2 application.

On the prod server I want all my routes to go via https, while in dev I want to be able to use http. How do I achieve that with symfony2 alone? I do not want to touch the webserver configuration.

I tried adding this in my routing.yml

myBundle:
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /
    schemes:  [https]

while having this in my routing_dev.yml:

myBundle:
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /
    schemes:  [http]

_main:
    resource: routing.yml

It still wants to go to https even in dev mode.

like image 840
k0pernikus Avatar asked Apr 22 '14 15:04

k0pernikus


3 Answers

You can define parameter for that. In app/config/config.yml define:

parameters:     httpProtocol: http 

Then in app/config/config_prod.yml:

parameters:     httpProtocol: https 

And in routing.yml change to:

myBundle:     resource: "@MyBundle/Controller/"     type:     annotation     prefix:   /     schemes:  ['%httpProtocol%'] 

Clear the cache (both prod and dev) and it should work.

like image 98
4 revs, 2 users 93% Avatar answered Sep 25 '22 18:09

4 revs, 2 users 93%


Change your app.php last lines like this:

if ($request->getScheme() === 'http') {     $urlRedirect = str_replace($request->getScheme(), 'https', $request->getUri());     $response = new RedirectResponse($urlRedirect); } else {     $response = $kernel->handle($request); }  $response->send();  $kernel->terminate($request, $response); 
like image 41
Michele Carino Avatar answered Sep 21 '22 18:09

Michele Carino


My first solution works fine, yet one should take care to not overwrite one own's routes in the routing_dev.yml. At the end of the file, I had

_main:
    resource: routing.yml

so all my bundle route was changed back to the https-scheme. Ordering the entries, so that my custom entry comes last resolved the issue.

like image 21
k0pernikus Avatar answered Sep 25 '22 18:09

k0pernikus