Magento 2: Check if Current URL & Frontend URL are Secure (https)

This article shows how we can check if current URL and frontend URl are secure (https) or not in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of StoreManagerInterface in the constructor of my module’s block class. app/code/Chapagain/HelloWorld/Block/HelloWorld.php <?php namespace Chapagain\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected … Read more

Magento: Check & Get Secure URL

Here are the code snippets to check for different secure URLs in Magento. Secure URL can be enabled from configuration settings (System -> Configuration -> GENERAL -> Web -> Secure). Enable Secure URL in Frontend System -> Configuration -> GENERAL -> Web -> Secure -> Use Secure URLs in Frontend = Yes Enable Secure URL … Read more

Magento: www or non-www URL redirecting to homepage

Problem: You have a Magento website with Base URL = http://example.com/ Working: http://example.com/category-one.html Not working: http://www.example.com/category-one.html (redirecting to homepage) The only difference in the above two URLs is that the second URL has “www” in it. Similary, you may have another Magento website with Base URL = http://www.example.com/ In this case, Working: http://www.example.com/category-one.html Not working: … Read more

Magento: How to remove index.php from URL?

In your Magento shop URL, if you see “index.php” and wanted to remove it then here is the solution. – First of all, apache module ‘mod_rewrite‘ should be enabled. Check apache’s httpd.conf file. – Then login to Magento admin panel – Go to System -> Configuration -> Web -> Search Engines Optimization -> Use Web … Read more

PHP: How to get Main or Base URL?

Suppose you are on a page : http://example.com/category/php/ Now, the base URL or the main URL for the above link is : http://example.com Similarly, in the case of local machine (localhost), Suppose you are in the page : http://localhost/myproject/index.php?id=8 Here, the base or main URL is : http://localhost/myproject Below is the code to get main … Read more

PHP: Simple and easy way to format URL string

Here, I will be showing you a simple and easy one line code to format URL string with PHP. By URL string, I mean the url key in any Search Engine Friendly URL. I have used preg_replace function to do so. preg_replace($pattern, $replacement, $string) preg_replace function performs a regular expression search and replace. This will … Read more

Magento: Easily add breadcrumbs to any page

Breadcrumbs are very useful for user navigation. Breadcrumbs for product page, category page, etc. are created by default Magento code. The following code will show breadcrumbs created by Magento. You can print the following code anywhere in php or phtml files. echo $this->getLayout()->getBlock('breadcrumbs')->toHtml(); You can create you own breadcrumbs as well. Like, you may need … Read more

Magento: Get current URL of the page

Getting URL of a current page you are visiting or working on is a common requirement in any web development project. There are various ways you can get the current URL of a page in Magento. Here are some :- From Mage_Core_Helper_Url class $currentUrl = Mage::helper('core/url')->getCurrentUrl(); From Mage_Core_Model_Store class $currentUrl = Mage::app()->getStore()->getCurrentUrl(false); If you want … Read more