【问题标题】:Symfony2: Redirecting from HTTPS-Route to HTTP-RouteSymfony2:从 HTTPS 路由重定向到 HTTP 路由
【发布时间】:2015-05-21 14:34:01
【问题描述】:

我有"/" 之类的基本路由http://example.com,如果有人调用https://example.com,我想将此路由重定向到http。我做错了什么?我是否必须将重定向选项放在其他地方?谢谢!

在我的路由 yml 中我有:

my_website:
    resource: "@MyWebsiteBundle/Controller/"
    type:     annotation
    prefix:   /
    defaults:
        route: home //<-- This is my name of the route "/" in my controller
        permanent: true

【问题讨论】:

标签: php symfony routing


【解决方案1】:

在你的 routing.yml 中:

home:
    path:     /
    defaults: { _controller: AcmeBundle:Default:index     }
    host: "www.example.com"
    schemes:  [http] # This is where the magic happens!

如果您想要所有路线,您可以转到您的 security.yml 并将 access_control 参数更改为此:

access_control:
    - { path: ^/, roles: [IS_AUTHENTICATED_ANONYMOUSLY], requires_channel: http }

如果您需要 https 来访问管理员:

access_control:
    - { path: ^/, roles: [IS_AUTHENTICATED_ANONYMOUSLY], requires_channel: http }
    - { path: ^/admin, roles: [ROLE_ADMIN], requires_channel: https }

更多信息可以在这里找到:http://symfony.com/doc/current/book/security.html

【讨论】:

  • 在我的控制器中我已经有了这个: /** * @Route("/", name="home", scheme= { "http" }) * @Template("MyWebsiteBundle:Home: index.html.twig") */ public function indexAction() { ...我已经定义了一个方案。所以我的 routing.yml 中不需要这个?
  • 是的,不要引用我的话,但我认为即使您使用注释,这也会出现在 routing.yml 中。我更喜欢 YAML 而不是注释,所以我对它不太熟悉。告诉我这是否有效,我很好奇结果。
  • 刚刚跟进,你最终找到答案了吗?
  • 当您尝试我的回答时,您是否清除了缓存?因为每当您在 routing.yml/config.yml 中播放时,cache:clear after 是非常重要的。如果它在产品中,则为缓存:clear --env=prod
【解决方案2】:

试试这个:

my_website:
    resource: "@MyWebsiteBundle/Controller/"
    type:     annotation
    prefix:   /
    requirements:
        _scheme:  http

【讨论】:

    猜你喜欢
    • 2011-04-28
    • 2013-01-20
    • 2016-05-26
    • 2015-11-24
    • 2018-10-22
    • 2014-12-27
    • 1970-01-01
    • 2012-01-21
    • 2015-06-16
    相关资源
    最近更新 更多