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
  • 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
    Commented Nov 29, 2017 at 5:28
  • @AmitBera can you explain more?
    – Sathish
    Commented Nov 29, 2017 at 6:51

2 Answers 2


You will need to rewrite the following classes for starter


like below


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
    Commented Nov 28, 2017 at 11:28
  • I have tried this its seems long process to achieve this, can we overwrite the core modules?
    – Sathish
    Commented Dec 1, 2017 at 13:15

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:


In dispatch() method

 // If pre-configured, check equality of base URL and requested URL

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



public function controllerFrontIntBefore($observer)
    return $this;


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(){
        $currentSubDomain = false;
        $getUrl  = $_SERVER["SERVER_NAME"];
        $split_url = explode(".", $getUrl);
        $subAccount = Mage::getModel('model/modelname')->getCollection()->addFieldToFilter('sub_domain', @strtolower($split_url[0]));
            $currentSubDomain = $subAccount->getFirstItem();
        Mage::register('cur_subdomain', $currentDist);
    }catch (Exception $e){
    $baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB,Mage::app()->getStore()->isCurrentlySecure());

public function setBaseUrlGlobal($url){
            $curDistKeyShop = $this->getSubDomain();
                $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){
    return $url;


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 ? Commented Dec 2, 2017 at 5:02
  • as i tested its working vicky may be some where i missed anything pls suggest your solutions
    – Sathish
    Commented Dec 2, 2017 at 12:47

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.