4

I am coming with question of how to create a wildcard subdomain in Magento 1.9.x without using store view. I know creating more store view will cause the issues in price indexing, flat tables, etc.,

I have countless subdomains.

I tried:

  • Created the wild card setup in cpanel using A record. *.example.com IN A 127.0.0.1
  • then when calling sub1.example.com & sub2.example.com & sub3.example.com its pointed to example.com.
  • added needed stuff in .htaccess.

Problems am having:

  • when we load sub1.example.com all urls needs to http://sub1.example.com/... but currently loads http://example.com/... I know its from db, so how to rewrite getUrl() for this case.

So I need a suggestion for load sub domains same like a main website without creating multiple store views.

Note: I did lot of digging in google & SO & ME.

Please suggest the possible ways to solve.

  • Don't thought it will be possible without store view – Amit Bera Nov 29 '17 at 5:28
  • @AmitBera can you explain more? – Sathish Nov 29 '17 at 6:51
3
+50

You will need to rewrite the following classes for starter

Mage_Page_Block_Html_Topmenu
Mage_Page_Block_Html_Footer
Mage_Catalog_Model_Product_Url
Mage_Catalog_Model_Layer_Filter_Item

like below

 <global>
        <blocks>
            <page>
                <rewrite>      
             <html_topmenu>Module_name_Block_Page_Html_Topmenu</html_topmenu>
                <html_footer>Module_name_Block_Page_Html_Footer</html_footer>
                </rewrite>
            </page>
        </blocks>
        <models>
            <catalog>
                <rewrite>     
           <product_url>Module_name_Model_Catalog_Product_Url</product_url> 
               <layer_filter_item>Module_name_Model_Catalog_Layer_Filter_Item</layer_filter_item>
                </rewrite>
            </catalog>
        </models>
 </global>

and this will give you Overall getUrl, getBaseUrl functions to put your incoming base url with actual db base url, if you don't have any static base urls in theme + base url redirection not enabled in db level then this will work for you.

Note: - having wildcard subdomain and same url/content will have effect in SEO

  • let me try and back to you ela – Sathish Nov 28 '17 at 11:28
  • I have tried this its seems long process to achieve this, can we overwrite the core modules? – Sathish Dec 1 '17 at 13:15
1

Finally find the solution for this after digging, I know its a hard code (not suggested this way) but i am in need to share my answer to ME and I hope will get the better answer when compared to this with out affecting core module.

Reason for why the sub domain redirected:

app/code/core/Mage/Core/Controller/Varien/Front.php

In dispatch() method

 // If pre-configured, check equality of base URL and requested URL
 $this->_checkBaseUrl($request);

Magento matching the requested url with base url, if not matched with (host or ..) they redirected to base url.

/**
 * Auto-redirect to base url (without SID) if the requested url doesn't match it.
 * By default this feature is enabled in configuration.
 *
 * @param Zend_Controller_Request_Http $request
 */
protected function _checkBaseUrl($request){
    .....
}

so plan to get the needed subdomain informtaion using this event controller_front_init_before before dispatch and set the new baseurl in cache

copied below files from core pool to local pool app/code/core/Mage/Core/Controller/Varien/Front.php to app/code/local/Mage/Core/Controller/Varien/Front.php

app/code/core/Mage/Core/Model/Store.php to app/code/local/Mage/Core/Model/Store.php

added event in config.xml

<events>
    <controller_front_init_before>
        <observers>
            <observer_name>
                <class>modelname/observer</class>
                <method>controllerFrontIntBefore</method>
            </observer_name>
        </observers>
    </controller_front_init_before>
</events>

observer.php

public function controllerFrontIntBefore($observer)
{
    Mage::helper('helperclass')->MethodSetUrl();
    return $this;
}

Data.php

public function coreSession(){
    return Mage::getSingleton('core/session');
}

public function getSubDomain(){
    return Mage::registry('cur_subdomain') ? strtolower(Mage::registry('cur_subdomain')->getSubDomain()) : false;
}

public function MethodSetUrl(){
    try{
        $currentSubDomain = false;
        $getUrl  = $_SERVER["SERVER_NAME"];
        $split_url = explode(".", $getUrl);
        $subAccount = Mage::getModel('model/modelname')->getCollection()->addFieldToFilter('sub_domain', @strtolower($split_url[0]));
        if($subAccount->count()){
            $currentSubDomain = $subAccount->getFirstItem();
            $this->coreSession()->setSubDomain($currentSubDomain);
        }
        Mage::register('cur_subdomain', $currentDist);
    }catch (Exception $e){
        Mage::throwException($e->getMessage());
    }
    $baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB,Mage::app()->getStore()->isCurrentlySecure());
}

public function setBaseUrlGlobal($url){
    try{
        if(Mage::registry('cur_subdomain')){
            $curDistKeyShop = $this->getSubDomain();
            if($url){
                $httpReg = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
                $splitUrl = explode('.', $url);
                $subDomain = str_replace($httpReg,'', $splitUrl['0']);
                $reqSubDomain = @explode(".", $_SERVER["SERVER_NAME"]);
                $reqSubDomain = str_replace($httpReg,'', $reqSubDomain);
                if($reqSubDomain && $reqSubDomain != $this->subDomain && $reqSubDomain != $this->Domain){
                    $splitUrl['0'] = $httpReg.$curDistKeyShop;
                    $url = implode('.', $splitUrl);
                }
            }
        }
    }catch (Exception $e){
        Mage::throwException($e->getMessage());
    }
    return $url;
}

app/code/local/Mage/Core/Model/Store.php

public function getBaseUrl($type = self::URL_TYPE_LINK, $secure = null) { ................ ................ $this->_baseUrlCache[$cacheKey] = Mage::helper('helpername')->setBaseUrlGlobal($this->_baseUrlCache[$cacheKey]); return $this->_baseUrlCache[$cacheKey]; }

I hope everything over for now.

And i have tried all the possible ways of getting baseurl in below ways

its returned as sub domain urls as excepted

$url = $this->getUrl();
$baseUrl= Mage::getBaseUrl();
$websiteUrl = Mage::getConfig()->getNode('web/unsecure/base_url', 'website', 'code');
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$skinUrl = $this->getSkinUrl('images/imagename.jpg');
$storeUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$homeUrl= Mage::helper('core/url')->getHomeUrl();

$request = $this->getRequest();
$urlWithoutParameters = $this->getBaseUrl() . $request->getRouteName() .DS. $request->getControllerName() .DS. $request->getActionName();

Looking for suggetsion for this answers and another possible ways too.

  • Is that working properly ? – Vignesh Bala Dec 2 '17 at 5:02
  • as i tested its working vicky may be some where i missed anything pls suggest your solutions – Sathish Dec 2 '17 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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