Get base URL in a Magento xml

Get base URL in a Magento xml

July 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get base URL in a Magento xml

Solving Relative URLs Issue in Magento XML

As a Magento developer, you often need to create links within your page layouts using XML files like page.xml. However, when dealing with URLs that should always point to the base root regardless of the current URL path, you might encounter issues due to relative paths.

Understanding the Problem

In your provided code snippet:

<block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links-follow.phtml">
    <action method="addLink" translate="label title">
        <label>Condiciones generales</label>
        <url>legal</url>
        <title>Condiciones generales</title>
    </action>
</block>

The <url> tag you’ve provided is just the relative path legal. If your Magento store is installed in a subdirectory (like /foo/) and someone visits /foo/products, this link will resolve to /foo/products/legal instead of the intended base root URL.

The Magento Base URL Solution

To ensure that your links always point to the correct base URL, you need to use the {{store direct_url="path"}} syntax in your XML file. This tells Magento to replace the “path” placeholder with the actual base URL of the current store during rendering.

Here’s how you can modify your code:

<block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links-follow.phtml">
    <action method="addLink" translate="label title">
        <label>Condiciones generales</label>
        <url>{{store direct_url="legal"}}</url>
        <title>Condiciones generales</title>
    </action>
</block>

In this updated version, we’re using {{store direct_url="legal"}} as the value for the <url> tag. When Magento renders this block, it will replace legal with the actual base URL of the current store.

For example, if your base URL is https://example.com/foo/, the generated markup will look like:

<a href="https://example.com/foo/legal">Condiciones generales</a>

Ensuring Consistency Across Stores

If you have multiple stores with different base URLs, using this method ensures that each store references its own correct URL. For instance, if you have a second store at https://es.example.com/bar/, the generated markup for that store would be:

<a href="https://es.example.com/bar/legal">Condiciones generales</a>

Best Practices and Further Reading

  1. Always use this method when adding links in your layout XML files to ensure they remain consistent regardless of base URL changes.

  2. Make sure you clear your cache after making these changes, as Magento caches the rendered blocks.

  3. For more advanced URL handling, consider looking into Magento’s Mage::getUrl() function or using the {store direct_url="path"} syntax within your PHTML templates (learn more at https://laravelcompany.com/magento-url-helpers-tutorial).

Conclusion

Handling URLs consistently across Magento is a common challenge. By utilizing the {{store direct_url="path"}} syntax in your XML layout files, you can ensure that your links always point to the correct base URL of your store, providing a seamless user experience regardless of where they are on your site.

Remember to clear your cache after making changes and always test thoroughly, especially when dealing with multiple stores or complex URL structures. Happy coding!

Catalog Themes Pricing Errors

Our network