如何强制https为prod但http为dev环境?

时间:2021-05-23 22:58:25

I have a symfony2 application.

我有一个symfony2应用程序。

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.

在prod服务器上我希望我的所有路由都通过https,而在开发中我希望能够使用http。我如何单独使用symfony2实现这一目标?我不想触摸网络服务器配置。

I tried adding this in my routing.yml

我尝试在我的routing.yml中添加它

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

while having this in my routing_dev.yml:

在我的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.

它仍然想在开发模式下转到https。

4 个解决方案

#1


58  

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

您可以为此定义参数。在app / config / config.yml中定义:

parameters:
    httpProtocol: http

Then in app/config/config_prod.yml:

然后在app / config / config_prod.yml中:

parameters:
    httpProtocol: https

And in routing.yml change to:

并在routing.yml中更改为:

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

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

清除缓存(prod和dev),它应该工作。

#2


2  

Change your app.php last lines like this:

像这样更改你的app.php最后一行:

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);

#3


0  

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

我的第一个解决方案工作正常,但是应该注意不要在routing_dev.yml中覆盖自己的路由。在文件的最后,我有

_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.

所以我所有的捆绑路由都改回了https-scheme。订购条目,以便我的自定义条目最后解决问题。

#4


-2  

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

我认为您可以在app_dev.php文件中检查该方案,并根据该方案将其重定向到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

或者也许能够在URL中为app_dev.php应用.htaccess中的一些更改

#1


58  

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

您可以为此定义参数。在app / config / config.yml中定义:

parameters:
    httpProtocol: http

Then in app/config/config_prod.yml:

然后在app / config / config_prod.yml中:

parameters:
    httpProtocol: https

And in routing.yml change to:

并在routing.yml中更改为:

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

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

清除缓存(prod和dev),它应该工作。

#2


2  

Change your app.php last lines like this:

像这样更改你的app.php最后一行:

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);

#3


0  

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

我的第一个解决方案工作正常,但是应该注意不要在routing_dev.yml中覆盖自己的路由。在文件的最后,我有

_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.

所以我所有的捆绑路由都改回了https-scheme。订购条目,以便我的自定义条目最后解决问题。

#4


-2  

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

我认为您可以在app_dev.php文件中检查该方案,并根据该方案将其重定向到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

或者也许能够在URL中为app_dev.php应用.htaccess中的一些更改