Created
November 9, 2012 21:22
-
-
Save jmather/4048356 to your computer and use it in GitHub Desktop.
Redirect to SSL only on given
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
ssl_manager.listener: | |
class: SSLManagementListener | |
arguments: [ 'example.com' ] | |
tags: | |
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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