Magento – How to add Secure URL for Custom rewrite URL In Magento custom Module

magento-1.8

Am trying to add secure URL to my custom module – below is my config.xml sample code

<frontend>
    <secure_url>
        <scheme>/scheme/</scheme>
    </secure_url>
    <routers>
        <scheme>
            <use>standard</use>
            <args>
                <module>Companyname_Scheme</module>
                <frontName>scheme</frontName>
            </args>
        </scheme>
    </routers> 
</frontend>
<global>
    <rewrite>            
        <myschemes>
            <from><![CDATA[/myschemes\/(.*)/]]></from>
            <to><![CDATA[scheme/user/$1/]]></to>
            <complete>1</complete>
        </myschemes>
    </rewrite>

My need is to make myschemes url to access only through https:// if the user forced to load on non http i.e., http:// .

If i add secure url for myschemes then above rewrite is not working. If i enter https://www.example.com/myschemes/ as url it is redirecting to https://www.example.com/scheme/user//

How can I achieve this? Please help.

Best Answer

Interesting question, I was curious how this worked myself so I did a little digging. If you take a look at the standard router (Mage_Core_Controller_Varien_Router_Standard) you'll see that it makes a determination if a route should be secure by using _checkShouldBeSecure, which will in turn call _shouldBeSecure and this will ultimately call Mage_Core_Model_Config::shouldUrlBeSecure.

From here you can see that the way to force a URL to be secure is to declare the path so in your modules XML code. To take an example from core, which you can see in app/code/core/Mage/Customer/etc/config.xml.

<frontend>
    <secure_url>
        <customer>/customer/</customer>
    </secure_url>
</frontend>