Magento 2: Adding Custom URLs with Store Base URL
In Magento 2, you can easily add custom links to your menus that include the store’s base URL. The {{base_url}} variable is available for use in menu items and other locations throughout the platform. Let me guide you through this process.
Understanding the Base URL Variable
The {{base_url}} variable in Magento 2 represents the base URL of your current store. It includes the protocol (http or https), domain name, and port number if specified. This variable is especially useful when creating links that need to be relative to the store’s root.
For example:
<a href="{{base_url}}my-custom-page.html">Go to Custom Page</a>
In this case, the link would resolve to https://example.com/my-custom-page.html (assuming your base URL is set to https://example.com).
Adding a Custom Link in Magento 2
To add a custom link with the store’s base URL, you can modify an existing menu or create a new one. Here are the steps:
-
Navigate to Content > Menus in your Magento 2 admin dashboard.
-
Click on the specific menu where you want to add the custom link (e.g., Top Menu).
-
Click on the Add New Link button.
-
In the URL Key field, enter a unique identifier for your page (this is not used by customers but helps Magento identify the link internally).
-
On the right side of the URL field, you’ll see a drop-down menu labeled Link to. Choose Custom URL.
-
In the URL field that appears, type in your custom path or use the {{base_url}} variable as needed.
-
Fill in the other fields like Title, Position, etc., as desired.
-
Click on Save Menu Item to apply the changes.
Troubleshooting: Why Isn’t {{base_url}} Working?
If you’re seeing issues with {{base_url}}, there are a few things to check:
-
Make sure your base URL is correctly configured in Stores > Configuration > General > Web.
-
Ensure that the cache is cleared or switched off during development to see immediate changes.
-
Double-check the syntax of your custom linkâmissing curly braces or incorrect parameter names can cause issues.
Best Practices for Magento 2 URLs
When creating URLs in Magento 2, consider these best practices:
- Use descriptive URL keys that include relevant keywords.
- Keep URL paths short and clean to improve SEO and user experience.
- Avoid using special characters or spaces in your URL keys whenever possible.
- Regularly monitor and update your URLs if you rename or move pages to preserve SEO value.
Conclusion
Adding custom links with the {{base_url}} variable is a straightforward process in Magento 2 that allows for dynamic, relative linking within your site’s menus. By following these steps and best practices, you can ensure your links are accurate, functional, and optimized for both users and search engines.
Remember to clear your cache regularly during development or use the Developer Mode for immediate visibility of changes. For more advanced Magento 2 customization and integration, consider exploring our Professional Services.
Happy coding! Let me know if you have any other questions about Magento 2 development.