Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
主页上的Magento“将代码存储到URL”_Magento_Mod Rewrite_Url Rewriting - Fatal编程技术网

主页上的Magento“将代码存储到URL”

主页上的Magento“将代码存储到URL”,magento,mod-rewrite,url-rewriting,Magento,Mod Rewrite,Url Rewriting,我打开了“管理->系统->配置->网络->url选项”选项存储url代码 问题是,如果我访问我的主页时没有商店代码,它就会工作。我的意思是这两个例子都有效: http://example.com/code/ 但第一个不带存储代码的url应该重定向到带存储代码的url。我曾尝试在htaccess中加入重写规则,但没有成功,我尝试了各种可能性 Magento内置重写规则似乎没有什么帮助-我尝试过重写/编码,但结果是带有/code/code url后缀。这种行为的原因可以在Mage\u Core\u

我打开了“管理->系统->配置->网络->url选项”选项存储url代码

问题是,如果我访问我的主页时没有商店代码,它就会工作。我的意思是这两个例子都有效: http://example.com/code/ 但第一个不带存储代码的url应该重定向到带存储代码的url。我曾尝试在htaccess中加入重写规则,但没有成功,我尝试了各种可能性


Magento内置重写规则似乎没有什么帮助-我尝试过重写/编码,但结果是带有/code/code url后缀。

这种行为的原因可以在Mage\u Core\u Model\u url\u rewrite::rewrite中找到。没有存储代码,基本URL没有重定向

下面是一个相当丑陋的解决方案,但它应该适用于您的情况。只要在请求URI中找不到当前存储代码,它将重定向到包含存储代码的基本URL:

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;
        }
    }

}
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>
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>

这种行为的原因可以在Mage_Core_Model_Url_Rewrite::Rewrite中找到。没有存储代码,基本URL没有重定向

下面是一个相当丑陋的解决方案,但它应该适用于您的情况。只要在请求URI中找不到当前存储代码,它将重定向到包含存储代码的基本URL:

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;
        }
    }

}
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>
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>

您是否已将“自动重定向到基本URL”设置为“是”?是的,但是我的基本URL不包含用于目录路径的代码,因此我认为不应该更改它。您是否已将“自动重定向到基本URL”设置为“是”?是的,但是我的基本URL不包含用于目录路径的代码,因此我认为不应该更改它。嗯,我不想用大锤敲碎坚果,但它的作用太大了。谢谢:回答得好!我稍微修改了代码,以重定向没有存储前缀的CMS页面:$targetUrl=rtrimage::getBaseUrl'/'$请求URI;谢谢,我会努力的,我遇到了同样的问题。嗯,我不想用大锤敲碎坚果,但它的作用太大了。谢谢:回答得好!我稍微修改了代码,以重定向没有存储前缀的CMS页面:$targetUrl=rtrimage::getBaseUrl'/'$请求URI;谢谢,我会努力的,我遇到了同样的问题。