从 OpenCart 中删除 index.php?route=common/home

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10813809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:06:41  来源:igfitidea点击:

Remove index.php?route=common/home from OpenCart

phpseoopencartsem

提问by TheBlackBenzKid

I currently have User SEO URL'sset to Yes in OpenCart Admin.

我目前User SEO URL's在 OpenCart Admin 中设置为 Yes。

System -> Settings -> Store -> Server -> User SEO URL's

System -> Settings -> Store -> Server -> User SEO URL's

So far, all tags and SEO links are working; the command has done the desired effect.

到目前为止,所有标签和 SEO 链接都有效;该命令已达到预期效果。

However for the homepage and a few other links; how do I remove:

但是对于主页和其他一些链接;我如何删除:

index.php?route=common/home

index.php?route=common/home

From the URL? Do I have to literally do a find and replace in the hardcode PHP files and risk upgrades or is there another way?

从网址?我是否必须在硬编码 PHP 文件和风险升级中进行查找和替换,还是有其他方法?

(without bloating performance i.e no poor amateur tools such as vQmod)

(没有膨胀性能,即没有 vQmod 等糟糕的业余工具)

回答by Jay Gilford

To simply remove that, you can do a basic replace in /catalog/controller/common/seo_url.php

要简单地删除它,您可以在 /catalog/controller/common/seo_url.php

Find:

找:

return $link;

Beforeit on a new line put:

新行之前放:

$link = str_replace('index.php?route=common/home', '', $link);

Edit by TheBlackBenzKid: If you want full SEO just use this line instead of the above:

由 TheBlackBenzKid 编辑:如果您想要完整的 SEO,只需使用此行而不是上面的:

$link = str_replace('index.php?route=', '', $link);

Also make sure SEO URLs is turned on in the Admin panel of the store.

还要确保在商店的管理面板中打开了 SEO URL。

回答by Victor Schr?der

The previous "solutions" are wrong because they are attacking the SEO URL translate. What you want is to deal with the URL generationinside OpenCart.

之前的“解决方案”是错误的,因为他们攻击的是SEO URL translate。您想要的是处理OpenCart 中的 URL生成

Let's keep it simple. Go to /system/library/url.phpand look at the public function link. Replace the function with this version:

让我们保持简单。去/system/library/url.php看看public function link。用这个版本替换函数:

public function link($route, $args = '', $connection = 'NONSSL') {
    if ('NONSSL' == $connection) {
        $url = $this->url;
    } else {
        $url = $this->ssl;  
    }

    if ('common/home' == $route) {
        if ($args) {
            $url .= '?' . str_replace('&', '&', '&' . ltrim($args, '&')); 
        }
    } else {
        $url .= 'index.php?route=' . $route;
        if ($args) {
            $url .= str_replace('&', '&', '&' . ltrim($args, '&')); 
        }
    }

    foreach ($this->rewrite as $rewrite) {
        $url = $rewrite->rewrite($url);
    }

    return $url;
}

Simple like that. I can't belive why this is not in OpenCart's core.

就这么简单。我无法相信为什么这不在 OpenCart 的核心中。

EDIT:

编辑:

I run some tests and if the SEO URLs are enabled, it becomes necessary to make one single edit in the /catalog/controller/common/seo_url.phpto avoid an "Undefined index" error.

我运行了一些测试,如果启用了 SEO URL,则有必要在 中进行一次编辑/catalog/controller/common/seo_url.php以避免“未定义索引”错误。

Inside public function rewrite, replace this line:

在里面public function rewrite,替换这一行:

parse_str($url_info['query'], $data);

With this one:

有了这个:

if (isset($url_info['query'])) parse_str($url_info['query'], $data);

Now it really works.

现在它确实有效。

回答by billynoah

I really like Victor Schr?der's solution above for it's simplicity. Thanks! I created a vQmod based on his code mods in case it would be helpful to anyone. here is the code:

我真的很喜欢上面 Victor Schr?der 的解决方案,因为它很简单。谢谢!我根据他的代码模块创建了一个 vQmod,以防它对任何人都有帮助。这是代码:

<modification>

    <file name="system/library/url.php">
        <operation>
            <search position="before"><![CDATA[$url .= 'index.php?route=' . $route;]]></search>
            <add><![CDATA[
                if ('common/home' == $route) {
                    if ($args) {
                        $url .= '?' . str_replace('&', '&amp;', '&' . ltrim($args, '&'));
                    }
                } else {
            ]]></add>
        </operation>
        <operation>
            <search position="before"><![CDATA[foreach ($this->rewrite as $rewrite) {]]></search>
            <add><![CDATA[
                }
            ]]></add>
        </operation>
    </file>

    <file name="catalog/controller/common/seo_url.php">
        <operation>
            <search position="replace"><![CDATA[parse_str($url_info['query'], $data);]]></search>
            <add><![CDATA[
                if (isset($url_info['query'])) parse_str($url_info['query'], $data);
            ]]></add>
        </operation>
    </file>

</modification>

回答by Kreativi

How about replacing a link in logo with <?php echo $base; ?>. It will make a link to base domain and will remove index.php?route=common/home.

将徽标中的链接替换为<?php echo $base; ?>. 它将建立一个指向基本域的链接,并将删除index.php?route=common/home.

回答by Vojta

Jay's solution doesn't work for me, after editing I get blank screen. So I made a new one:

Jay 的解决方案对我不起作用,编辑后我得到黑屏。所以我做了一个新的:

Put the line before:

把这行放在前面:

return $link;

Instead of after:

而不是之后:

public function rewrite($link) {

回答by frostymarvelous

So, I'm using 1.5.5.1 and no one answer on this question solved my problem. However, combining the answers from @Jay Gilford, @TheBlackBenzKidand @rkaartikeyenI came up with a fully working solution.

所以,我使用的是 1.5.5.1 并且没有人回答这个问题解决了我的问题。然而,结合@Jay Gilford@TheBlackBenzKid@rkaartikeyen的答案,我想出了一个完全可行的解决方案。

Remember to enable seo urls as shown by @TheBlackBenzKid.

请记住启用@TheBlackBenzKid所示的 seo 网址。

An explanation can be found below the code.

可以在代码下方找到解释。

[php]
class ControllerCommonSeoUrl extends Controller {
    public function index() {
        // Add rewrite to url class
        if ($this->config->get('config_seo_url')) {
            $this->url->addRewrite($this);
        }

        // Decode URL
        if (isset($this->request->get['_route_'])) {
            $parts = explode('/', $this->request->get['_route_']);

            foreach ($parts as $part) {
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");

                if ($query->num_rows) {
                    $url = explode('=', $query->row['query']);

                    if ($url[0] == 'product_id') {
                        $this->request->get['product_id'] = $url[1];
                    }

                    if ($url[0] == 'category_id') {
                        if (!isset($this->request->get['path'])) {
                            $this->request->get['path'] = $url[1];
                        } else {
                            $this->request->get['path'] .= '_' . $url[1];
                        }
                    }   

                    if ($url[0] == 'manufacturer_id') {
                        $this->request->get['manufacturer_id'] = $url[1];
                    }

                    if ($url[0] == 'information_id') {
                        $this->request->get['information_id'] = $url[1];
                    }   
                } else {
                    $this->request->get['route'] = 'error/not_found';   
                }
            }

            if (isset($this->request->get['product_id'])) {
                $this->request->get['route'] = 'product/product';
            } elseif (isset($this->request->get['path'])) {
                $this->request->get['route'] = 'product/category';
            } elseif (isset($this->request->get['manufacturer_id'])) {
                $this->request->get['route'] = 'product/manufacturer/info';
            } elseif (isset($this->request->get['information_id'])) {
                $this->request->get['route'] = 'information/information';
            }else {
                $this->request->get['route'] = $this->request->get['_route_'];
            }

            if (isset($this->request->get['route'])) {
                return $this->forward($this->request->get['route']);
            }
        }
    }

    public function rewrite($link) {
        $url_info = parse_url(str_replace('&', '&', $link));

        $url = ''; 

        $data = array();

        parse_str($url_info['query'], $data);

        foreach ($data as $key => $value) {
            if (isset($data['route'])) {
                if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");

                    if ($query->num_rows) {
                        $url .= '/' . $query->row['keyword'];

                        unset($data[$key]);
                    }                   
                } elseif ($key == 'path') {
                    $categories = explode('_', $value);

                    foreach ($categories as $category) {
                        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");

                        if ($query->num_rows) {
                            $url .= '/' . $query->row['keyword'];
                        }                           
                    }

                    unset($data[$key]);
                }
            }
        }

        if ($url) {
            unset($data['route']);

            $query = '';

            if ($data) {
                foreach ($data as $key => $value) {
                    $query .= '&' . $key . '=' . $value;
                }

                if ($query) {
                    $query = '?' . trim($query, '&');
                }
            }

            return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query;
        } else {
            $link = str_replace('index.php?route=', '', $link);
            return $link;
        }
    }   
}

Apparently, @Jay Gilfordand @TheBlackBenzKidsolve the issue of the urls being properly written on the page.

显然,@Jay Gilford@TheBlackBenzKid解决了在页面上正确写入网址的问题。

Line 113

$link = str_replace('index.php?route=', '', $link);

But it seems to break the urls since the Controller can't find the pages and therefore reverts to the error page.

但它似乎破坏了 url,因为控制器找不到页面,因此恢复到错误页面。

Line 38 - 40

} else {
    $this->request->get['route'] = 'error/not_found';   
}

@rkaartikeyen'ssolution solves this problem by setting the current route to the requested route

@rkaartikeyen 的解决方案通过将当前路由设置为请求的路由来解决这个问题

Line 51 - 53

else {
    $this->request->get['route'] = $this->request->get['_route_'];
}

回答by frostymarvelous

Step 01. Enable USE SEO URL's in your store server settings

步骤 01。在您的商店服务器设置中启用 USE SEO URL

Go to the “System” and click on “Settings”. Locate the store you want to alter and click the “Edit” link on the right. Finally click the “Server” tab and set the SEO URL's radio to “Yes” and save your settings.

转到“系统”,然后单击“设置”。找到您要更改的商店,然后单击右侧的“编辑”链接。最后单击“服务器”选项卡并将搜索引擎优化 URL 的单选设置为“是”并保存您的设置。

Note: The keywords will not be created for you automatically. You must also have Apache mod_rewrite turned on. Most web hosts will have this on by default. Step 3 of this tutorial will explain how to add the keywords.

注意:不会自动为您创建关键字。您还必须打开 Apache mod_rewrite。默认情况下,大多数网络主机都会启用此功能。本教程的第 3 步将解释如何添加关键字。

Step 02. Change some content in your .htaccess file to make your homepage SEO URL Friendly

步骤 02. 更改 .htaccess 文件中的一些内容,使您的主页 SEO URL 友好

Go to your host control panel and edit your .htaccess file. Sometimes this file is hidden in order to unhide it you can click on your web host control panel file manager and tick the show hidden files option followed by the “go” button.

转到您的主机控制面板并编辑您的 .htaccess 文件。有时为了取消隐藏此文件,您可以单击您的 Web 主机控制面板文件管理器并勾选显示隐藏文件选项,然后单击“开始”按钮。

Once you locate the .htaccess file change the following lines:

找到 .htaccess 文件后,更改以下几行:

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_= [L,QSA]

To:

到:

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_= [L,QSA]
RewriteCond %{QUERY_STRING} ^route=common/home$
RewriteRule ^index\.php$ http://www.yourwebsite.co.uk? [R=301,L]

If you do the step above your home page will change from something like this: http://yourwebsite.com/index.php?route=common/hometo this: http://yourwebsite.com

如果您执行上述步骤,您的主页将从以下内容更改:http: //yourwebsite.com/index.php?route=common/home 到此:http: //yourwebsite.com

Step 03. Enter SEO Keywords for URL's manuallyFinally, you need to enter SEO keywords for every page, information, product and category you want to have URL rewrite. You can find the field for the SEO Keywords under the Data tab when editing and creating items.

步骤 03. 手动输入 URL 的 SEO 关键字最后,您需要为要重写 URL 的每个页面、信息、产品和类别输入 SEO 关键字。编辑和创建项目时,您可以在数据选项卡下找到 SEO 关键字字段。

Once you have entered the SEO Keywords your URL's will be working.

一旦您输入了 SEO 关键字,您的 URL 就会起作用。

Once you have followed all these instructions you can begin to reap the benefits of higher rankings, increased traffic and more customers.

一旦您按照所有这些说明进行操作,您就可以开始享受更高排名、增加流量和更多客户的好处。

回答by Frank

I came late but my solution could be useful for others (tested on Opencart 2.0.3.1):

我来晚了,但我的解决方案可能对其他人有用(在 Opencart 2.0.3.1 上测试):

Open your MySQL console and run this query (change YOURDATABASE with your db name):

打开您的 MySQL 控制台并运行此查询(使用您的数据库名称更改 YOURDATABASE):

INSERT INTO `YOURDATABASE`.`url_alias` (`url_alias_id`, `query`, `keyword`) VALUES (NULL, 'common/home', ' ');

How it works:

这个怎么运作:

The trick consists in adding a WHITE SPACE (' ') for the column "keyword", if you insert an empty string ('') this workaround doesn't work and the url rewriter will return again index.php?route=common/home.

诀窍在于为“关键字”列添加一个空格(''),如果您插入一个空字符串(''),则此解决方法不起作用并且 url 重写器将再次返回 index.php?route=common/家。

回答by Shadi Namrouti

For me, the simplest way to do it:

对我来说,最简单的方法是:

1- Go to /system/library/url.php

1-去 /system/library/url.php

2- Locate function link(

2- 定位 function link(

3- Above return $url;put the following line:

3-上面return $url;放以下行:

$url = str_replace('index.php?route=common/home', '', $url);

回答by rkaartikeyan

/catalog/controller/common/seo_url.php

/catalog/controller/common/seo_url.php

Open the file and replace the below code

打开文件并替换下面的代码

<?php
class ControllerCommonSeoUrl extends Controller {
    public function index() {
        // Add rewrite to url class
        if ($this->config->get('config_seo_url')) {
            $this->url->addRewrite($this);
        }

        // Decode URL
        if (isset($this->request->get['_route_'])) {
            $parts = explode('/', $this->request->get['_route_']);

            foreach ($parts as $part) {

                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");

                if ($query->num_rows) {
                    $url = explode('=', $query->row['query']);

                    if ($url[0] == 'product_id') {
                        $this->request->get['product_id'] = $url[1];
                    }

                    if ($url[0] == 'category_id') {
                        if (!isset($this->request->get['path'])) {
                            $this->request->get['path'] = $url[1];
                        } else {
                            $this->request->get['path'] .= '_' . $url[1];
                        }
                    }   

                    if ($url[0] == 'manufacturer_id') {
                        $this->request->get['manufacturer_id'] = $url[1];
                    }

                    if ($url[0] == 'information_id') {
                        $this->request->get['information_id'] = $url[1];
                    }   
                } else {
                    $this->request->get['route'] = 'error/not_found';   
                }
            }

            if (isset($this->request->get['product_id'])) {
                $this->request->get['route'] = 'product/product';
            } elseif (isset($this->request->get['path'])) {
                $this->request->get['route'] = 'product/category';
            } elseif (isset($this->request->get['manufacturer_id'])) {
                $this->request->get['route'] = 'product/manufacturer/info';
            } elseif (isset($this->request->get['information_id'])) {
                $this->request->get['route'] = 'information/information';
            }else {
                $this->request->get['route'] = $this->request->get['_route_'];
            }

            if (isset($this->request->get['route'])) {
                return $this->forward($this->request->get['route']);
            }
        }
    }

    public function rewrite($link) {
        if ($this->config->get('config_seo_url')) {
            $url_data = parse_url(str_replace('&amp;', '&', $link));
            $url = ''; 

            $data = array();

            parse_str($url_data['query'], $data);
            foreach ($data as $key => $value) {

                if (isset($data['route'])) {
                    if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
                        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");

                        if ($query->num_rows) {
                            $url .= '/' . $query->row['keyword'];

                            unset($data[$key]);
                        }                   
                    } elseif ($key == 'path') {
                        $categories = explode('_', $value);

                        foreach ($categories as $category) {
                            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");

                            if ($query->num_rows) {
                                $url .= '/' . $query->row['keyword'];
                            }                           
                        }

                        unset($data[$key]);
                    }else {
                        $url .= '/'.$value;
                    }
                }
            }

            if ($url) {
                unset($data['route']);

                $query = '';

                if ($data) {
                    foreach ($data as $key => $value) {
                        $query .= '&' . $key . '=' . $value;
                    }

                    if ($query) {
                        $query = '?' . trim($query, '&');
                    }
                }

                return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query;
            } else {
                return $link;
            }
        } else {
            return $link;
        }       
    }   
}
?>

------------------WHERE THE CHANGE -------------------------

------------------变化的地方-------------------------

The below Line i added in Public Function index() also Public Function rewrite.

我在公共函数索引()中添加的以下行也是公共函数重写。

else {
this->request->get['route'] = $this->request->get['_route_'];
}

index() function Perform like this

index() 函数 像这样执行

  1. Get the query from htaccss "?route=$1" (Ex: the url look like site.com/shirts)
  2. so the routeis shirts
  3. now the function check the "shirts" is found in url_alias table if its found then its make a get varible like index.php?categoryID=Value from url_alias Table.
  4. Or else if there are no record in url_alias table for shirts, then its checks weather it is some other pages like information or manufacturer if its found it make a get variable Example index.php?route=information/information or index.php?route=product/manufacturer/info.
  5. But its not check is it found in Layouts. So i added the Code in else block that set the routeinto get variable like this index.php?route=<route>
  6. So in index() functions its works fine. Like Same i did in rewrite function.
  1. 从 htaccss "? route=$1"获取查询(例如:url 看起来像 site.com/shirts)
  2. 所以路线是衬衫
  3. 现在,函数检查 url_alias 表中是否找到了“衬衫”,如果找到了,那么它会生成一个像 index.php?categoryID=Value 来自 url_alias 表的变量。
  4. 否则,如果 url_alias 表中没有衬衫的记录,那么它会检查它是否是其他一些页面,如信息或制造商,如果它发现它使获取变量示例 index.php?route=information/information 或 index.php?route =产品/制造商/信息。
  5. 但它不检查是否在布局中找到。所以我在 else 块中添加了代码,将路由设置为 get 变量,如 index.php?route=< route>
  6. 所以在 index() 函数中它工作正常。就像我在重写功能中所做的一样。