Magento – Redirect/rewrite home page domain.com to store view domain.com/en/

.htaccesshomeredirecturl-rewrite

I have enabled Add Store Code to Urls to access the different store views of my Magento website, but now www.domain.com and www.domain.com/en/ have the same content. To avoid duplicate content I would like to redirect www.domain.com (just the home page) to www.domain.com/en/.

How can I achieve this by using .htaccess or any other intelligent method? Thank you.

Best Answer

I was looking for this as well. I just can't understand why Magento does not do this by default. I could not get this to work by using .htaccess

But here's what looks like an intelligent method by Daniel Sloof that seems to work perfectly fine for my store, so kudo's to him for developing this little module!

Note: The original <code> for the module can be found on stackoverflow.com, but I'll post it below for easier future reference.

  1. app/code/local/Danslo/RedirectToStore/Model/Observer.php

    <?php
    
    class Danslo_RedirectToStore_Model_Observer
    {
    
        public function redirectToStore($observer)
        {
            $request    = $observer->getFront()->getRequest();
            $storeCode  = Mage::app()->getStore()->getCode();
            $requestUri = $request->getRequestUri();
    
            if (strpos($requestUri, $storeCode) === false) {
                $targetUrl = $request->getBaseUrl() . '/' . $storeCode;
                header('HTTP/1.1 301 Moved Permanently');
                header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
                header('Pragma: no-cache');
                header('Location: ' . $targetUrl);
                exit;
            }
        }
    
    }
    
  2. app/code/local/Danslo/RedirectToStore/etc/config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <global>
            <events>
                <controller_front_init_before>
                    <observers>
                        <redirect_to_store>
                            <class>Danslo_RedirectToStore_Model_Observer</class>
                            <method>redirectToStore</method>
                            <type>singleton</type>
                        </redirect_to_store>
                    </observers>
                </controller_front_init_before>
            </events>
        </global>
    </config>
    
  3. app/etc/modules/Danslo_RedirectToStore.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <Danslo_RedirectToStore>
                <active>true</active>
                <codePool>local</codePool>
            </Danslo_RedirectToStore>
        </modules>
    </config>
    

Note 2: To add a trailing slash to the URL (like Magento does when clicking your stores logo) I made a small modification to Daniels Observer.php file and changed

$targetUrl = $request->getBaseUrl() . '/' . $storeCode;

into

$targetUrl = $request->getBaseUrl() . '/' . $storeCode . '/';

In addition to this, I also added to store code of my default store view to index.php

so

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

now looks like

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'en';

This does not seem to be entirely necessary, but I ran into this when Googling for a solution...

Hope this helps someone. If there is a better option or room for improvement, then please comment!

Related Topic