How to disable localized URLs?

By default, Sylius uses localised URLs, meaning that they contain the /locale prefix to specify the current locale. For example, when the "English (United States)" locale is selected in the channel, the homepage URL might look like this: {hostname}/en_US/.

If you prefer to use URLs without the locale prefix, you can disable this feature by following these steps.


1. Update the Routing Configuration

To stop Sylius from generating localised URLs, you need to customize the application routing in config/routes/sylius_shop.yaml.

🔍 What to Change

Use this diff to identify what should be removed or updated in config/routes/sylius_shop.yaml:

# config/routes/sylius_shop.yaml
    
    sylius_shop:
        resource: "@SyliusShopBundle/Resources/config/routing.yml"
-       prefix: /{_locale}
-       requirements:
-          _locale: ^[A-Za-z]{2,4}(_([A-Za-z]{4}|[0-9]{3}))?(_([A-Za-z]{2}|[0-9]{3}))?$
    
    sylius_shop_payum:
        resource: "@SyliusPayumBundle/Resources/config/routing/integrations/sylius_shop.yaml"
    
-    sylius_shop_default_locale:
-        path: /
-        methods: [GET]
-        defaults:
-            _controller: sylius.controller.shop.locale_switch:switchAction

✅ Final Configuration (After Applying the Diff)

After the changes, your sylius_shop.yaml file should look like this:

# config/routes/sylius_shop.yaml

sylius_shop:
    resource: "@SyliusShopBundle/Resources/config/routing.yml"

sylius_shop_payum:
    resource: "@SyliusPayumBundle/Resources/config/routing/integrations/sylius_shop.yaml"

By making this change, the locale will no longer be included in the URLs generated by Sylius.


2. Use Storage-Based Locale Switching

Configure the locale_switcher:

# config/packages/_sylius.yaml

sylius_shop:
    locale_switcher: storage

This ensures that locale selection persists across user sessions without being part of the URL.


3. Adjust the Shop URL Regex for Security

Sylius uses the sylius.security.shop_regex parameter to define which paths are part of the shop firewall. Without the locale prefix, you need to adapt this regex.

🛡️ Add to config/packages/security.yaml

At the top of the file, insert:

# config/packages/security.yaml

parameters:
    sylius.security.shop_regex: "^(?:/(?!%sylius_admin.path_name%|api/.*|api$|media/.*)[^/]++)?"

This update ensures the shop routes are matched correctly without the locale prefix.


✅ Final Result

Before (Localized URL)
After (Clean URL)

By completing these steps:

  • URLs like /en_US/product/foo become /product/foo.

  • Locale persistence is handled in session or cookies, not in the URL.

  • Routing and security rules continue to work correctly.

Last updated

Was this helpful?