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

后端 未结 4 1390
孤街浪徒
孤街浪徒 2021-02-02 11:13

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 a

相关标签:
4条回答
  • 2021-02-02 11:15

    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.

    0 讨论(0)
  • 2021-02-02 11:16

    I think you can check the scheme in app_dev.php file and base on that redirect it to https

    $url = parse_url($_SERVER['HTTP_REFERER']);
    if ($url['scheme'] == 'http') {
       header("Location: https://{$_SERVER['SERVER_NAME']}{$_SERVER['PHP_SELF']}");
    }
    

    Or maybe able to apply some changes in .htaccess for app_dev.php in URL

    0 讨论(0)
  • 2021-02-02 11:21

    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.

    0 讨论(0)
  • 2021-02-02 11:31

    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);
    
    0 讨论(0)
提交回复
热议问题