11

I've enabled System > Config > Web > Add Store Code to URLs and I've setup a new Store View for Canada (/ca/).

I'd like to ideally have example.com/ca/ load the Canada Store View, but still have example.com (without the default store code in there) for the default store.

I've noticed that the home page works just fine in this regard, but when I go into a product page, Magento automatically includes the store code in the URL.

The main reason that I'm hesitant to change our current URLs for our default store is just because I'd hate to see them lose any link juice. I know that theoretically as long as I setup 301 redirects, I should be fine, but I think I've read somewhere that a small amount of juice is still lost.

Also, I think this is probably somewhat better from a usability perspective. If the bulk of your traffic is to your default store, and your customers are used to the regular URL, why bother them with the additional /default/ (or /us/ or whatever) in the URL.

I've tried disabling Add Store Code to URLs and simply changing the Base URL for my Canada Store View to example.com/ca/, but that didn't seem to do anything. I'm assuming it needs the setting enabled in order to pick up the store code in the URL and do anything at all with it.

5 Answers 5

9

I didn't become happy using Add Store Code to URLs. The main reason was that we couldn't have several store views using the same language (see my question). Besides that I didn't like I have to use a bad store view code just because of the URL.

I did a small test using the frequently recommended "create a subfolder and create symlinks" tip and it may work. Please check if you like it.

  • Create a subfolder /ca/
  • Inside the subfolder create symbolic links to
    • ../app
    • ../errors
    • ../includes
    • ../index.php
    • ../js
    • ../lib
    • ../media
    • ../skin
    • ../var
    • (add other directories if you want/need to)
  • edit your vHost config for /ca/ (or copy .htaccess to /ca/ and edit it) to

    • Set the rewrite base:

      RewriteBase /ca/

    • Initialise the correct store view based on the URL. There are many ways to do it, one may be:

      RewriteCond %{REQUEST_URI} ^/ca/.* [NC,OR]
      RewriteCond %{REQUEST_URI} ^/ca$ [NC]
      RewriteRule .* - [E=MAGE_RUN_CODE:ca]
      RewriteCond %{ENV:REDIRECT_MAGE_RUN_CODE} (.+)
      RewriteRule .* - [E=MAGE_RUN_CODE:%1,E=MAGE_RUN_TYPE:store]`
      

It worked for me:

Creating folders and symlinks isn't fun but at least you don't have to edit core files.

5
  • 2
    You only have to make symlinks for media, js and skin and then edit the Mage class path to ../app/Mage.php. As far as I know that should be enough Commented Sep 22, 2013 at 19:13
  • 2
    Yes, if you want to edit core files. Commented Sep 23, 2013 at 7:55
  • @MatthiasZeis sorry for taking so long to accept! Sometimes I lose track of SE notifications and it's hard to mark them as unread. Feel free to ping me on twitter in the future if I take forever to mark an answer as accepted! At least with twitter you can favorite a tweet in order to remember to come back to it. Sorry and thanks again for the help here! Great answer! Commented Feb 10, 2014 at 22:58
  • No worries! I'm glad I could help you. Commented Feb 11, 2014 at 14:02
  • 2
    Great advise! Two possible simplifications: 1) Since you create separate vHost configurations or .htaccess files for the subdirectory, you can initialize the store view straightforward without conditions: SetEnv MAGE_STORE_VIEW ca. RewriteBase on the other hand is usually not necessary. 2) It should be enough to set symlinks to index.php and app, if you only set the Base Link URL to /ca/ and leave the default Base URL configuration Commented Apr 22, 2014 at 16:16
2

Based on the answer of mzeis and fschmengler's comment, here is a KISS way of doing it:

  1. Create a subdirectory ca: mkdir ca

  2. Create a symlink to the app directory: ln -s ../app/ ca/app

  3. Create a symlink to the index.php file: ln -s ../index.php/ ca/index.php

  4. Create a .htaccess file in the ca directory: touch ca/.htaccess with the following content:

     SetEnv MAGE_RUN_CODE ca
    
     RewriteEngine On
     RewriteBase /ca/
    
     # solves the issue that the environment variable MAGE_RUN_CODE is only available as REDIRECT_MAGE_RUN_CODE
     # see http://stackoverflow.com/a/10128290/719023 for details
     RewriteOptions inherit
    
  5. Under System > Configuration > Web, set the Base Link URL to {{unsecure_base_url}}ca/ and {{secure_base_url}}ca/ respectively.

If, for some reason, the RewriteOptions inherit part is not possible for you, you can also simply copy the default mod_rewrite part into your sub-directory .htaccess file instead.

This solution should work as good as the one from mzeis, but is a lot smaller and easier.

2
  • 1
    I know this is very old, and I am not 100% sure, but I believe that RewriteBase /en/ should be changed to RewriteBase /ca/ in the example above.
    – Louis B.
    Commented Mar 24, 2022 at 11:45
  • Good catch, @LouisB.! Fixed.
    – Simon
    Commented Mar 25, 2022 at 7:41
1

User clockworkgeek created the Magento-Store-Codes module just for this use-case.

I have tested this on CE 1.9.2.3 with a single website setup with multiple store views, and it seemed to work perfectly!

See also: Magento : How to hide the default Store View code from the url

0

another option is to use a subdomain per store. Then you just setup Apache to tell magento which "website/store" to use.

This way you can have multiple "stores" and "views" under the subdomain websites all in the same magento intall.

  • settings are in System>Configuration>General/Web
  • Unsecure . Base URL
  • Secure . Base URL
  • Cookie . Domain

this is the example.com store Apache config

<VirtualHost *:80>
    AllowEncodedSlashes On

    ServerName example.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/

    DirectoryIndex index.php

    <Directory "/var/www/html/" >
        AllowOverride All
    </Directory>

    SetEnv MAGE_RUN_CODE "default"
    SetEnv MAGE_RUN_TYPE "Website"
</VirtualHost>

this is the additional "ca" store Apache config

<VirtualHost *:80>
    AllowEncodedSlashes On

    ServerName ca.example.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/

    DirectoryIndex index.php

    <Directory "/var/www/html/" >
        AllowOverride All
    </Directory>

    SetEnv MAGE_RUN_CODE "ca"
    SetEnv MAGE_RUN_TYPE "Website"
</VirtualHost>
0

Juste add this to one of your system.xml file (in app/code/local module):

<sections>
    <web>
        <groups>
            <url>
                <fields>
                    <use_store>
                        <show_in_store>1</show_in_store>
                    </use_store>
                </fields>
            </url>
        </groups>
    </web>
</sections>

This way you can choose the option "Add Store Code to URLs" per store view.

4
  • You are able to select it. But are you sure, that it actually works? Commented Dec 28, 2015 at 12:16
  • Yes, tested on magento 1.9.x and magento EE 1.13.x
    – nverhoye
    Commented Dec 29, 2015 at 12:59
  • It would have been great if this actually worked, but I have not been able to make this work in a setting with one website and multiple store views. Tested on CE 1.9.2.3. Commented Feb 23, 2016 at 15:33
  • not worked for me Commented Apr 12, 2017 at 6:53

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.