【问题标题】:Remove index.php?route=common/home from OpenCart从 OpenCart 中删除 index.php?route=common/home
【发布时间】:2012-06-04 13:00:34
【问题描述】:

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

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

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

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

index.php?route=common/home

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

(没有臃肿的性能,即没有像 vQmod 这样的糟糕的业余工具)

【问题讨论】:

  • 据我所知 100% 硬代码 - 更糟糕的是如果 OpenCart 环境运行在 Nginx 上,因此您需要编写许多手动函数 - 而且它并不像查找和替换 /common/home - 更多的功能! Repped..因为我也想回答
  • 你试过用htaccess删除吗?
  • @PLB 你不能这样做,因为我使用的是 Nginx,除了 .htaccess 只重定向或重写 - 你仍然必须删除代码中的 index.php?route=。
  • 我发现这样做的唯一方法是modify the code
  • 你可以查看我的答案。解决于 1.5.5.1

标签: php seo opencart


【解决方案1】:

在 Opencart 3 中,如果你想重定向所有丑陋的 url,比如

index.php?route=common/home
index.php?route=account/register
index.php?route=product/product&path=244&product_id=481

您需要将漂亮的网址添加到Admin -> Design -> SEO Urls,类似于

然后这样做:在catalog/controller/startup/seo_url.php 中,在函数index 之后

if ($this->config->get('config_seo_url')) {
   $this->url->addRewrite($this);
}

添加这个

if (isset($this->request->get['route'])) {
    if (DOMAIN_ROOT . $this->request->server['REQUEST_URI'] != $this->rewrite(DOMAIN_ROOT . $this->request->server['REQUEST_URI'])) {
        $this->response->redirect($this->rewrite(DOMAIN_ROOT . $this->request->server['REQUEST_URI']));
    }
}
elseif (strpos($this->request->server['REQUEST_URI'], 'index') !== false) {
    $this->response->redirect(HTTPS_SERVER);
}

添加最后一步是在config.php中定义DOMAIN_ROOT

define('DOMAIN_ROOT', 'https://www.somedomain.org'); // no slash, no subfolder

对我来说效果很好

【讨论】:

    【解决方案2】:

    OpenCart 3.x

    首先在您网站的根文件夹中将 htaccess.txt 重命名为 .htaccess

    然后转到系统 > 设置 > 您的商店。编辑。打开标签 Server 并将 Use SEO URLs 设置为 Yes

    现在打开 catalog/controller/startup/seo_url.php

    查找} elseif ($key == 'path') {

    在前面添加

    } elseif ($key == 'route') {
      $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE `query` = '" . $this->db->escape($value) . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'");
     
      if ($query->num_rows && $query->row['keyword']) {
        $url .= '/' . $query->row['keyword'];
        unset($data[$key]);
      } else if ($data['route'] == "common/home") { 
        $url .= '/'; 
      } 
    

    现在/index.php?route=common/home 变成了/

    现在您可以使用 account/logininformation/contact 等结构在 Design > SEO URL 中设置 SEO URL

    【讨论】:

      【解决方案3】:

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

      1- 转到/system/library/url.php

      2- 找到function link(

      3- 在return $url; 上方添加以下行:

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

      【讨论】:

      • @TheBlackBenzKid 是的,在版本 3.0.3.3 上测试
      【解决方案4】:

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

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

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

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

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

      找到 .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_=$1 [L,QSA]
      

      收件人:

      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_=$1 [L,QSA]
      RewriteCond %{QUERY_STRING} ^route=common/home$
      RewriteRule ^index\.php$ http://www.yourwebsite.co.uk? [R=301,L]
      

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

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

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

      一旦您遵循所有这些说明,您就可以开始获得更高排名、增加流量和更多客户的好处。

      【讨论】:

      • 我认为重点是能够在每次创建产品/页面时自动执行此操作,而不是手动执行。
      【解决方案5】:

      要从网址中删除 index.php?route=,我只是建议编辑 .htaccess 文件。

      只需添加这个:

      RewriteCond %{THE_REQUEST} \ /index\.php\?_route_=?([^&\ ]*)
      RewriteRule ^ /%1? [L,R]
      

      我没有遇到任何问题。请记住,您需要启用 RewriteEngine。寻找这条线:RewriteEngine On。如果不存在,则在上面的代码之前过去。

      【讨论】:

      • 这不会将其从 PHP 文件中删除,但是会通过硬编码将其包含在其中。
      【解决方案6】:

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

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

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

      它是如何工作的:

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

      【讨论】:

      • 1.5.5.1.2 工作,但keyword 有空格我点击徽标时在我的网址末尾有%20,所以我删除了空格。
      【解决方案7】:

      在文件 \system\library\response.php 在公共函数 output() 的开头添加下一行

      if (!defined('HTTP_CATALOG')) $this->output = str_replace(array('index.php?route=common/home', '?route=common/home'), '', $this->output);
      

      【讨论】:

        【解决方案8】:

        【讨论】:

          【解决方案9】:

          我为此创建了一个 VQMOD。在此处免费下载:http://www.opencart.com/index.php?route=extension/extension/info&extension_id=14683

          效果很好。

          【讨论】:

            【解决方案10】:

            我真的很喜欢上面 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>
            

            【讨论】:

              【解决方案11】:

              之前的“解决方案”是错误的,因为它们攻击的是 SEO URL翻译。您想要的是处理 OpenCart 中的 URL generation

              让我们保持简单。转到/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('&', '&amp;', '&' . ltrim($args, '&')); 
                      }
                  } else {
                      $url .= 'index.php?route=' . $route;
                      if ($args) {
                          $url .= str_replace('&', '&amp;', '&' . ltrim($args, '&')); 
                      }
                  }
              
                  foreach ($this->rewrite as $rewrite) {
                      $url = $rewrite->rewrite($url);
                  }
              
                  return $url;
              }
              

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

              编辑:

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

              public function rewrite内,替换这一行:

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

              有了这个:

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

              现在它真的有效了。

              【讨论】:

              • 这将在不启用 SEO URL 的情况下使用 OR,并将保留查询字符串参数
              • opencart 1.5.5.1.2 - 我在 url 看到 myshop/index.php?route
              • @VitalyZdanevich,那是什么超频版本?是正式版吗?您确定您已打开 SEO 网址吗?无论如何,我真的建议您升级,因为不再支持这样的旧版本并且存在很多安全问题。
              • 嘿维克多,谢谢!我尽了一切努力,但什么也没发生。这适用于 3.0 吗?
              【解决方案12】:

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

              记得启用@TheBlackBenzKid所示的seo url。

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

              [php] 类 ControllerCommonSeoUrl 扩展控制器 { 公共函数索引(){ // 添加重写到 url 类 if ($this->config->get('config_seo_url')) { $this->url->addRewrite($this); } // 解码网址 if (isset($this->request->get['_route_'])) { $parts = explode('/', $this->request->get['_route_']); foreach ($parts 作为 $part) { $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE 关键字 = '" . $this->db->e​​scape($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]; } 别的 { $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]; } } 别的 { $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'] = '信息/信息'; }别的 { $this->request->get['route'] = $this->request->get['_route_']; } if (isset($this->request->get['route'])) { 返回 $this->forward($this->request->get['route']); } } } 公共函数重写($链接){ $url_info = parse_url(str_replace('&', '&', $link)); $url = ''; $数据 = 数组(); 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'] == '信息/信息' && $key == 'information_id')) { $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->e​​scape($key . '=' . (int)$价值) 。 ”'”); if ($query->num_rows) { $url .= '/' 。 $query->row['keyword']; 未设置($data[$key]); } } elseif ($key == '路径') { $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']; } } 未设置($data[$key]); } } } 如果($url){ 未设置($data['route']); $查询 = ''; 如果($数据){ foreach ($data as $key => $value) { $查询 .= '&' 。 $键。 '=' 。 $价值; } 如果($查询){ $查询 = '?' .修剪($查询,'&'); } } 返回 $url_info['scheme'] 。 '://' 。 $url_info['host'] 。 (isset($url_info['port']) ? ':' . $url_info['port'] : '') 。 str_replace('/index.php', '', $url_info['path']) 。 $网址。 $查询; } 别的 { $link = str_replace('index.php?route=', '', $link); 返回$链接; } } }

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

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

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

              38 - 40 号线 } 别的 { $this->request->get['route'] = 'error/not_found'; }

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

              51 - 53 号线 别的 { $this->request->get['route'] = $this->request->get['_route_']; }

              【讨论】:

              • 你用的是什么版本?另外,我很久以前就停止使用OC了。
              【解决方案13】:

              要简单地删除它,您可以在 /catalog/controller/common/seo_url.php 中进行基本替换

              查找:

              return $link;
              

              之前将它放在一个新行上:

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

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

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

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

              【讨论】:

              • 所以将删除所有index.php?route=,因为基本上这就是所需要的。
              • 您添加的内容将确定路线,但这可能会产生影响。例如,首先您需要在链接到新 url 时将其转换回路由。其次,如果页面有其他参数而不仅仅是路由会发生什么。假设您使用搜索页面,您将拥有 product/search&amp;page=2&amp;query=something 这是一个无效的 URL,所以我建议您不要使用上面的内容
              • 这不适用于 1.5.5.1 有人得到解决方案吗? vQmod 也不起作用。作者没有把它落实到位是多么令人讨厌。
              • @JayGilford 我是说上面的解决方案和这个重写的 vQmod 解决方案不存在或工作; 1.5.5.1 似乎没有工作
              • @Umair 对。这个问题是在 OC2 发布前 2.5 年提出的
              【解决方案14】:

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

              把这一行放在前面:

              return $link;
              

              而不是之后:

              public function rewrite($link) {
              

              【讨论】:

              • 有趣。你用的是什么版本?
              • 谢谢。感谢反馈
              • 这不适用于 1.5.5.1 有人得到解决方案吗? vQmod 也不起作用。作者没有把它落实到位是多么令人讨厌。
              • 我将在这里奖励赏金,因为这是 JayGilford 使用的解决方案。还要确保在管理面板中打开了 SEO URL。..
              • Put the line before: return $link; Instead of after: public function rewrite($link) { 我不明白......你想说什么......请帮忙
              【解决方案15】:

              /catalog/controller/common/seo_url.php

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

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

              ------------------ 改变的地方 ---------------- ---------

              我在 Public Function index() 中添加的以下行也是 Public Function rewrite.

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

              index() 函数这样执行

              1. 从 htaccss "?route=$1" 获取查询(例如:url 看起来像 site.com/shirts)
              2. 所以路线是衬衫
              3. 现在函数检查“衬衫”是否在 url_alias 表中找到,如果找到,那么它会从 url_alias 表中生成一个类似于 index.php?categoryID=Value 的变量。
              4. 否则,如果 url_alias 表中没有衬衫的记录,则它会检查是否是其他一些页面,例如信息或制造商,如果它发现它会生成一个获取变量 Example index.php?route=information/information 或 index。 php?route=product/manufacturer/info.
              5. 但它不检查它是否在布局中找到。所以我在 else 块中添加了将 route 设置为 get 变量的代码,例如 index.php?route=route>
              6. 所以在 index() 函数中它工作正常。 就像我在重写函数中所做的一样。

              【讨论】:

                【解决方案16】:

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

                【讨论】:

                • 它肯定会删除,但是 index.php 的其余部分呢?route=common/home url?
                • 这是一个好点。我喜欢它并且 +1,但这不是一个完整的解决方案。我从来不知道$base
                • 不完整的解决方案..单击导航栏中的某些内容也会转到 index.php?blabla
                猜你喜欢
                • 1970-01-01
                • 2012-11-30
                • 1970-01-01
                • 2021-01-22
                • 2021-02-28
                • 1970-01-01
                • 2014-09-20
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多