0

I have to set the whole website using https. I've already set on use in frontend area option and forced the unsecure base url to use https.

Then, in order to permanent redirect old url to https, I've set the code below in .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Unfortunately every http request is just redirected to the home page instead of the equivalent https page.

Any idea about solving this issue? thanks very much

3 Answers 3

1

Copy file app/code/core/Mage/Core/Controller/Varien/Front.php to app/code/local/Mage/Core/Controller/Varien/Front.php

Modify method protected function _checkBaseUrl($request) from

protected function _checkBaseUrl($request)
    {
        if (!Mage::isInstalled() || $request->getPost() || strtolower($request->getMethod()) == 'post') {
            return;
        }

        $redirectCode = (int)Mage::getStoreConfig('web/url/redirect_to_base');
        if (!$redirectCode) {
            return;
        } elseif ($redirectCode != 301) {
            $redirectCode = 302;
        }

        if ($this->_isAdminFrontNameMatched($request)) {
            return;
        }

        $baseUrl = Mage::getBaseUrl(
            Mage_Core_Model_Store::URL_TYPE_WEB,
            Mage::app()->getStore()->isCurrentlySecure()
        );
        if (!$baseUrl) {
            return;
        }

        $uri = @parse_url($baseUrl);
        $requestUri = $request->getRequestUri() ? $request->getRequestUri() : '/';
        if (isset($uri['scheme']) && $uri['scheme'] != $request->getScheme()
            || isset($uri['host']) && $uri['host'] != $request->getHttpHost()
            || isset($uri['path']) && strpos($requestUri, $uri['path']) === false
        ) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect($baseUrl, $redirectCode)
                ->sendResponse();
            exit;
        }
    }


to

protected function _checkBaseUrl($request)
    {
        if (!Mage::isInstalled() || $request->getPost() || strtolower($request->getMethod()) == 'post') {
            return;
        }

        $redirectCode = (int)Mage::getStoreConfig('web/url/redirect_to_base');
        if (!$redirectCode) {
            return;
        } elseif ($redirectCode != 301) {
            $redirectCode = 302;
        }

        if ($this->_isAdminFrontNameMatched($request)) {
            return;
        }

        $baseUrl = Mage::getBaseUrl(
            Mage_Core_Model_Store::URL_TYPE_WEB,
            Mage::app()->getStore()->isCurrentlySecure()
        );
        if (!$baseUrl) {
            return;
        }

        $uri = @parse_url($baseUrl);
        $requestUri = $request->getRequestUri() ? $request->getRequestUri() : '/';
        $newUrl = rtrim($baseUrl, '/') . $requestUri; // PD-April-170
        if (isset($uri['scheme']) && $uri['scheme'] != $request->getScheme()
            || isset($uri['host']) && $uri['host'] != $request->getHttpHost()
            || isset($uri['path']) && strpos($requestUri, $uri['path']) === false
        ) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect($newUrl, $redirectCode)
                ->sendResponse();
            exit;
        }
    }


Under system->configuration->web->Url Options , set the flag Auto-redirect to Base URL to Yes.

1
  • 1
    No need for a core hack here. These kinds of things are a combination of base URL configuration, cached 301 heaters and not fully understanding what is going on. Use curl to test your redirects and use different redirect codes did Magento and apache to see which does what.
    – user4351
    Nov 11, 2015 at 8:40
1

Go to system->configuration->web->Url Options and set the flag Auto-redirect to Base URL to No.

1
  • 1
    Unfortunately doesn't work. I tried to clean magento cache, varnish and memcache.
    – apedic
    Aug 7, 2015 at 8:48
1

Have you tried to use this in your .htaccess

 RewriteCond %{HTTPS} off
 RewriteCond %{HTTP:X-Forwarded-Proto} !https
 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.