Skip to content

Instantly share code, notes, and snippets.

@jmather
Created November 9, 2012 21:22
Show Gist options
  • Save jmather/4048356 to your computer and use it in GitHub Desktop.
Save jmather/4048356 to your computer and use it in GitHub Desktop.
Redirect to SSL only on given
services:
ssl_manager.listener:
class: SSLManagementListener
arguments: [ 'example.com' ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
<?php
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
class SSLManagementListener
{
private $domain;
public function __construct($domain)
{
$this->domain = strtolower($domain);
}
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
return;
}
$request = $event->getRequest();
if (strtolower($request->getHost()) == $this->domain && strtolower($request->getScheme()) != 'https')
{
$response = new RedirectResponse('https://'.$request->getHost().$request->getRequestUri(), 302);
$event->setResponse($response);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment