3

I'm using Magento with multiple stores and store views (1 website, 4 stores with one store view each). I activated the option "add store code to urls" and can access my different stores via http://example.com/storecode/, which works fine. However i have one store code which should be named after two words for SEO purposes. Unfortunately Magento only allows to use an underscore (_) instead of a dash (-) as store code:

The store code may contain only letters (a-z), numbers (0-9) or underscore(_), the first character must be a letter.

As dashes are recommended to separate words in URLs, I'm looking for a method to use a dash as store code. Overriding Magento's validation is no problem, but I'm wondering if there's any special reason dashes are disabled here. Does anybody have an idea?

Thanks, Mathias

Community
  • 1
  • 1
maff
  • 763
  • 2
  • 7
  • 16
  • An explanation: A code is an identifier and normally represents one word. Therefore it consist of characters of the word-character class. Also not starting with a number sounds like a named identifier. So your two-words use-case is not applicable on the code, as it represents one word. Therefore no non-word-characters are allowed. Lowercase has been chosen to further simplify the code. So you've found you can use a single word within the path-component of the URI and you've found out that the store-code is not suitable for two words. An URL-rewrite and output filter can handle this. – hakre Jul 18 '15 at 21:52
  • Hi Mathias. I'm actually facing to the same requirement and would be interested by your experience. Which solution did you choose finally? – DarkCowboy Apr 03 '17 at 11:27

1 Answers1

4

You can always try and make a copy of Mage_Core_Model_Mysql4_Store in app/code/local and modify the regular expression to allow the dash. What may be the reason for it is the way Magento interprets _ as dividers or spaces in naming conventions.

I would presume there is a reason, but what that is specifically I'm not sure. If you do make the changes I would recommend doing it on a copy of your codebase/magento before hand to determine if there's any repercussions before doing it on a production site.

protected function _beforeSave(Mage_Core_Model_Abstract $model)
{
    if(!preg_match('/^[a-z]+[a-z0-9_\-]*$/',$model->getCode())) {
        Mage::throwException(
            Mage::helper('core')->__('The store code may contain only letters (a-z), numbers (0-9) or underscore(_), the first character must be a letter'));
    }

    return $this;
}
B00MER
  • 5,471
  • 1
  • 24
  • 41