Redirects

Aside from using the Redirect App in Backstage (see this article), you may want to have an advanced HTTP Redirect mechanism. Some reasons may be: Migrating URL patterns from a legacy system to Frontastic (for example, Product URLs from /products/<sku>/<slug> to /p/<slug>/<sku>), using canonical link redirects, or redirecting from one domain to another (for example, [yourdomain.com](<http://yourdomain.com>) to www.yourdomain.com).

In an ideal world, this would all be done in the web server, but this configuration is part of the Frontastic platform so can't be adjusted to individual needs. We evaluated the possibility of allowing adjustments to provisioning but the potential security impact to our infrastructure was too high.

So we have multiple ways to easily do this in our backend-for-frontend stack, even though it may feel a little strange to handle such mechanics in PHP rather than the web server.

Path Pattern Redirects

You can use Symfony, our backend framework, to create a redirect from an old URL schema to a new one if you want to change parameters or change hard-coded parts of a URL. You shouldn't use this method if you need to include any coding logic.

You'll need to configure a route that matches the URL paths to be redirected. For example:

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
    <route id="Customer.Legacy.redirectStorePage" path="/store-{store}/{city}" methods="GET">
        <default key="_controller">Frontastic\Customer\Controller\LegacyRedirectController::redirectStorePageAction</default>
    </route></routes>
</routes>

For more information on how to do this as well as redirecting using a route, see this link.

Path Redirects with Logic

In cases where you need to parse parameters, add new parameters from a data source or any other manipulations that can't be handled by configuration, you'll need to implement your own redirect controller.

As with Path Pattern Redirects, you'll need to configure a route that matches the URL paths to be redirected. For example:

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
    <route id="Customer.Legacy.redirectStorePage" path="/store-{store}/{city}" methods="GET">
        <default key="_controller">Frontastic\Customer\Controller\LegacyRedirectController::redirectStorePageAction</default>
    </route></routes>
</routes>

Then, you'll need to implement the controller which actually performs the redirect:

<?php

namespace Frontastic\Customer\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Frontastic\Customer\Domain\StorefinderService;

class LegacyRedirectController extends Controller
{
    public function redirectStorePageAction(string $store, $filiale = null, string $city = null)
    {
        if ($store === 'finder') {
            return new RedirectResponse(
                $this->get('router')->generate(
                    'Customer.Store.finder'
                ),
                301
            );
        } else {
            if ($filiale !== null) {
                $store = $filiale;
            }

            $storeFinderService = $this->get(StorefinderService::class);
            $redirectStore = $storeFinderService->getOneStoreByStoreID($store);    
      
            return new RedirectResponse(
                $this->get('router')->generate(
                    'Customer.Store.store',
                    ['slug' => $redirectStore->slug]
                ),
                301
            );
        }
    }
}

More Global Redirects

If you need to redirect on the basis of domain or any other higher level redirect, you can use an Event Listener in the Symfony Framework and react to any incoming request early in the process. You can find out how to do this here.


‹ Back to Article List

Next Article ›

Stream URL Handling Concept

Still need help? Contact Us Contact Us