Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento getProductUrl() is always returning HTTP

Tags:

magento

Even when I am on HTTPS, Magento's getProductUrl() always seem to return an HTTP URL. Any ways I can make this auto-switch to HTTPS? (or have it return relative protocol url).

I would say it's a rather 'standard' configuration.

Base URL is http://example.com/

Secure URL is https://example.com/

Use Secure URLs in Frontend is No

Base Link URL is {{unsecure_base_url}}

I am aware I could change the above to {{secure_base_url}} however I do not want to force a change from HTTP to HTTPS, I only need it to stay relative.

like image 314
Francis Kim Avatar asked Sep 20 '12 23:09

Francis Kim


2 Answers

The main Magento's benefit, is that you can do anything you want with it :) So, yes, you can output HTTPS product urls or relative ones. However, before choosing a solution, let's consider the Magento authors' vision.

The HTTPS for frontend is designed to work only for specific areas like Customer account, Payment methods, Checkout, etc. There is nothing so private about products, which makes it necessary to be viewed via HTTPS as well.

By default Magento doesn't use HTTPS even for pages, mentioned above. In order to turn HTTPS on, the "Use Secure URLs in Frontend" option must be set to "Yes". Which, as described, will engage HTTPS only in limited set of pages that contain some private data.

So the best solution for you depends on specifics of the store, you are developing.

1) If you want to engage HTTPS for all the pages on frontend - then the best solution is to put "https://..." into "Base URL" option for "Unsecure" web url configuration.

2) If you want to turn on HTTPS only for product links and only for a limited number of pages, then you can override templates of that pages in order to put there relative urls. The actual code can be implemented in any way you like, even the simplest already proposed way is ok:

echo trim($_product->getProductUrl(),'http:')

3) If you want to engage HTTPS for all the product links at frontend - then the best way is to override Mage_Catalog_Module_Product_Url model and change method getUrl() - you need to put there

$routeParams['_secure'] = true;

This will produce all the product urls with HTTPS protocol.

4) If you need to show HTTP product links only at HTTP pages and HTTPS product links only at HTTPS pages, then you can use method 3) with a more sophisticated logic: check the protocol of current page before setting '_secure' parameter.

Hope, it helps.

like image 183
Andrey Tserkus Avatar answered Nov 12 '22 02:11

Andrey Tserkus


I just did it the primitive way, sometimes it works best:

echo trim($_product->getProductUrl(),'http:')
like image 32
Francis Kim Avatar answered Nov 12 '22 02:11

Francis Kim