Reputation: 75
I want to redirect this url http://www.businessbid.ae/stagging/web/feedback
to http://www.businessbid.ae
. What can I do?
Upvotes: 9
Views: 9659
Reputation: 17759
You should create a listener to listen for the onKernelExceptionEvent
.
In that you can check for the 404 status code and set the redirect response from that.
AppBundle\EventListener\Redirect404ToHomepageListener
namespace AppBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Routing\RouterInterface;
class Redirect404ToHomepageListener
{
/**
* @var RouterInterface
*/
private $router;
/**
* @var RouterInterface $router
*/
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
/**
* @var GetResponseForExceptionEvent $event
* @return null
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
// If not a HttpNotFoundException ignore
if (!$event->getException() instanceof NotFoundHttpException) {
return;
}
// Create redirect response with url for the home page
$response = new RedirectResponse($this->router->generate('home_page'));
// Set the response to be processed
$event->setResponse($response);
}
}
services.yml
services:
app.listener.redirect_404_to_homepage:
class: AppBundle\EventListener\Redirect404ToHomepageListener
arguments:
- "@router"
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Upvotes: 18
Reputation: 4244
You need to override default Exception Controller. IMHO better solution is to do that job in .htaccess
or nginx config.
Upvotes: 1
Reputation: 421
Try this into your controller:
$this->redirect($this->generateUrl('route_name')));
Upvotes: 0