2012-05-30 110 views
25

我目前已將User SEO URL's在OpenCart Admin中設置爲Yes。從OpenCart中刪除index.php?route = common/home

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

到目前爲止,所有的標籤和SEO鏈接工作;該命令已經取得了預期的效果。

但是,對於主頁和其他一些鏈接;我該如何去除?

的index.php路線=普通/家庭

從URL?我是否需要從字面上查找和替換硬編碼PHP文件和風險升級,還是有其他方法?

(無腹脹性能即沒有差的業餘工具,如vQmod)

+0

據我所知,100%的硬代碼 - 更糟的是,如果OpenCart Enviroment在Nginx上運行,所以你需要編寫許多手動函數 - 而且它不像找到並替換那個/ common /家 - 更多的功能本身!複製..因爲我也想回答 – AlphaApp

+0

你試過用'htaccess'去除? – Leri

+0

@PLB你不能這樣做,因爲我正在使用Nginx,除此之外.htaccess只重定向或重寫 - 你仍然需要刪除代碼中的index.php?route =。 – TheBlackBenzKid

回答

26

簡單地刪除,你可以做/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在商店的管理員控制檯打開。

+0

因此,將刪除所有'index.php?route ='本質上是需要的。 – TheBlackBenzKid

+0

編輯,我已經在上面添加了另一個解決方案。謝謝你。 – TheBlackBenzKid

+3

您添加的內容將確定路線,但這可能會產生影響。例如,首先,您需要在鏈接到該路由時將新的url轉換回路由。其次,如果頁面具有其他參數而不僅僅是路線,會發生什麼。假設你使用搜索頁面,你將會有'product/search&page = 2&query = something'這是一個無效的URL,所以我會建議你反對上面的代碼 –

3

如何更換徽標中的鏈接<?php echo $base; ?>。它將建立到基本域的鏈接並將刪除index.php?route=common/home

+3

它會刪除那個肯定的,但是index.php的其餘部分呢?route = common/home url? –

+0

這是一個很好的觀點。我喜歡它和+1,但它不是一個完整的解決方案。我從來不知道'$ base' – TheBlackBenzKid

+0

不完整的解決方案..點擊導航欄中的內容也會去index.php?blabla –

0

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

---------------- -更改地址-------------------------

在公共函數索引()中添加的以下行也公開功能重寫。

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

指數()函數執行這樣

  1. 獲取從htaccss查詢(例如:網址看起來像site.com/shirts) 「路線 = $ 1嗎?」
  2. 所以路由是襯衫
  3. 現在函數檢查「襯衫」是否在url_alias表中找到,如果找到了,那麼它的make就像var index.php中的變量一樣?categoryID = url_alias表中的值。
  4. 或者,如果在襯衫的url_alias表中沒有記錄,那麼它的檢查天氣是一些其他頁面,如信息或製造商,如果它發現它使一個變量例如index.php?route = information/information或index。 PHP→路線=產品/生產/信息。
  5. 但它沒有檢查是否在佈局中找到。所以我說在其他塊)的代碼,設置路線到GET變量像這樣的index.php?路徑= < 路線>
  6. 所以在指數(函數其工作正常。 像我一樣在重寫功能。
+0

不工作對不起 –

3

傑的解決方案不適合我,編輯後我得到空白屏幕。所以,我提出一個新問題:

之前放線:

return $link; 

而不是後:

public function rewrite($link) { 
+0

有趣。你使用什麼版本? – TheBlackBenzKid

+1

我使用v1.5.5.1先生。 – Vojta

+0

謝謝。欣賞反饋 – TheBlackBenzKid

3

所以,我使用1.5.5.1關於這個問題沒有一個答案解決了我的問題。 但是,結合@Jay Gilford,@TheBlackBenzKid@rkaartikeyen的答案,我想出了一個完全可行的解決方案。

請記住啓用seo網址,如@TheBlackBenzKid所示。

解釋可以在代碼下面找到。

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

顯然,@Jay Gilford@TheBlackBenzKid解決正確寫入頁面上的URL的問題。

 
Line 113 

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

但它似乎打破了網址,因爲控制器無法找到頁面,因此恢復到錯誤頁面。

 
Line 38 - 40 

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

@rkaartikeyen's解決當前的路由設置請求路由

 
Line 51 - 53 

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

不適合我... –

+0

你使用的是什麼版本?另外,我很久以前就不再使用OC了。 – frostymarvelous

7

先前的「解決方案」來解決這個問題是錯誤的,因爲他們正在攻擊SEO URL 翻譯。你想要的是在OpenCart內部處理URL 世代

讓我們保持簡單。轉到/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網址的啓用,有必要使一個單一的編輯在/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); 

現在,它確實有效。

+2

這將適用於或沒有啓用SEO網址,並將保留查詢字符串參數 –

+0

opencart 1.5.5.1.2 - 我看到'myshop/index.php?路線'在url –

+0

@VitalyZdanevich,什麼樣的OC版本是這樣的?這是正式版嗎?你確定你有SEO網址開啓了嗎?無論如何,我真的建議你升級,因爲這樣一箇舊版本不再支持,並有很多安全問題。 –

8

我真的很喜歡VictorSchröder的解決方案,因爲它很簡單。謝謝!我創建了一個基於他的代碼mods的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> 
0

移除代碼在文件\系統\圖書館\ response.php爲我工作 在公共函數輸出()的開頭添加下一行

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

我來晚了,但我的解決方案可能是其他人(對Opencart的2.0.3.1測試)有用:

(與你的數據庫名稱變更YOURDATABASE)打開你的MySQL控制檯,並運行此查詢:

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

工作原理:

訣竅在於添加空格(「‘)爲列‘關鍵字’,如果插入一個空字符串(’」)這種解決方法不起作用, url重寫器會再次返回index.php?route = common/home。

+0

在'1.5.5.1.2'工作,但在'keyword'處有空格當我點擊徽標時,我的URL末尾有'%20',所以我刪除了空格。 –

-1

要從網址中刪除index.php?route=我只是推薦編輯.htaccess文件。

只需補充一點:

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

我遇到沒有任何問題。請記住,您需要啓用RewriteEngine。尋找這條線:RewriteEngine On。如果不存在,則在上面的代碼之前將其過去。

+0

這不會將它從PHP文件中移除,但是它們會被硬編碼以將其放入其中。 – TheBlackBenzKid

1

步驟01.允許使用的搜索引擎優化的URL在你的存儲服務器設置

轉到「系統」,然後點擊「設置」。找到您要更改的商店,然後點擊右側的「修改」鏈接。最後點擊「服務器」標籤,並將SEO URL的收音機設置爲「是」並保存設置。

注意:關鍵字不會自動爲您創建。你還必須打開Apache mod_rewrite。大多數網絡主機默認都會有這個功能。本教程的第3步將解釋如何添加關鍵字。

第02步。更改您的一些內容。htaccess文件,使您的主頁SEO友好

轉到您的主機控制面板和編輯您的.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關鍵字你的網址將工作。

一旦你遵循了所有這些說明,你就可以開始享受更高的排名,增加的流量和更多的客戶的好處。