搜尋引擎網址友善化

From Joomla! Documentation

This page is a translated version of the page Search Engine Friendly URLs and the translation is 57% complete.
Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Kiswahili • ‎Nederlands • ‎català • ‎español • ‎français • ‎português • ‎português do Brasil • ‎فارسی • ‎中文(台灣)‎ • ‎日本語

搜尋引擎網址友善化 (SEF), 人類可讀 或稱為 乾淨 URL 是指 URL 能解釋特定頁面的內容,對於人類或是搜尋引擎而言是有意義的。從Joomla 1.5起,Joomla! 就可以建立並解析任何格式的 URLs ,也包含 SEF URL。這並不需要依賴網站伺服器的 URL rewriting 是啟用的,因此即使 Joomla! 執行於 Apache + mod_rewrite 模組以外的網站伺服器都能用。SEF URL 會依據特定的固定格式,然而用戶可以為每一個部分來定義 short descriptive text (別名)

在內部,SEF URL 的本機部分 (在域名之後的部份)稱為路由。因此建立以及執行 SEF URL也是名為 路由,相關的程式碼也稱為路由

範例資料庫中的文章"Welcome to Joomla!" URL 是一個不錯的路由範例。

  • 在 SEF URL 沒有打開時, URL 是 http://www.example.com/index.php?option=com_content&view=article&id=1:welcome-to-joomla&catid=1:latest-news&Itemid=50
  • 在 SEF URL 打開但是 mod_rewrite 關閉時,URL 是 http://www.example.com/index.php/the-­news/1-­latest­-news/1­-welcome­-to­-joomla
  • 在 SEF URL 和 mod_rewrite 都打開時,URL 是 http://www.example.com/the-­news/1­-latest-­news/1-­welcome-­to­-joomla

開啟全站設定中的搜尋引擎網址友善化 選項,即可啟用搜尋引擎網址友善化。自Joomla 1.6起這個選項預設是啟用的。更多資訊請參見 Enabling Search Engine Friendly (SEF) URLs

常見問題

URL 當中的數目字是什麼東東?

比較啟用前後的 URL ,我們可以在原本的 URL 看到數字

http://www.example.com/index.php?option=com_content&view=article&id=1:welcome-to-joomla&catid=1:latest-news&Itemid=50

在新的 URL 當中也有:

http://www.example.com/the-­news/1­-latest-­news/1-­welcome-­to­-joomla

這個數字是 Joomla! 需要的參數,以取得 internal URL 並顯示您需要看到的頁面。 (在這個案例中,前面的數字1是目錄的ID,第二個數字1是文章的ID。)

在網址列中已經看不到 index.php 了,所以我我可以把檔案刪除掉嗎?

不可以!雖然網址列看不到 index.php,但是 mod_rewrite, but internally the mod_rewrite will only redirect to the original path without showing it to you.

什麼是 別名 值?它如何被建立?

Alias is listed beneath the Title field in Articles, Categories, Sections and Menu Items. Joomla! can automatically create the alias for you. An automatic alias begins with the title. All upper case letters are changed to lower case. Spaces and special characters not allowed in a URL; they are changed to dashes.

我想要自行指定別名的值

如果您不喜歡 Joomla! 提供的別名,您可以使用, you can enter a value of your choosing into that field. 好些人相信,在URL中妥善使用關鍵字會改善搜尋引擎排名。You can do so by including those keywords in your title, and allowing Joomla! to create the alias, or by creating the alias yourself.

別名是如何使用於 URL當中?

以選單項目來說,Joomla! 會使用別名來當做網址後綴(URL slug)。假設您使用了頭兩個 SEF URL 項目,然後您建立一個名為 Products 的選單項目,那麼您的 URL 將會是 example.com/products

Joomla! also uses the primary key values of data within the URL to help the router navigate to the correct page. Continuing with the previous example, if your products menu item was for an Article­/Category Blog, the link for the Article Title and/or Read More link would have three parts:

  • The menu item URL - example.com/products;
  • Plus, the primary key for the Category and the Category alias - 32-fruit;
  • Plus, the primary key for the Article and the Article alias - 1-apple;

The complete URL is: http://example.com/products/32-fruit/1-apple

How can I get rid of the numbers in the SEF URLs?

The numbers in the SEF URL are needed by Joomla!'s router to know how to direct site traffic. Once the router logic stabilizes, simple third party system plugins can be developed to augment the router capabilities by allowing more choice. At that time, numbers will likely be removed from the URL.

Route Formats and the Routing Mechanism

This section describes Joomla!'s core (built-in) routing mechanism. Routing extensions may change the way routes are created on your system.

路由格式

To describe the Joomla! routing mechanism in more detail, we first need to pin down what we refer to as a route. Suppose that Joomla! has been installed in http://example.com/sites/first/. The installation path is generally referred to as the base URL. A possible example URL is http://example.com/sites/first/products/32-fruit/1-apple. The first part of this URL is the aforementioned base URL, and neither Joomla! nor any component router can create URLs with a different first part. The second part, products/32-fruit/1-apple, is a route, consisting of three segments.

The first segment of a route is, for regular URLs, the alias of a menu item. The SEF URL is said to be routed through that menu item. The other segments are determined entirely by the router of the component that provides the type of the menu item. The Category - Blog menu item type, for example, is provided by the Content component, and therefore that component's router is responsible for building and parsing the remaining segments.

It is also possible (for extensions) to ask the system to create a route without supplying a menu item to route through. In that case, the system will usually decide to create a special route that has the word component as the first segment. These routes are created using a fixed format: the name of the component (without the leading com_) is selected as the second segment and any parameters as the other segments.

限制

It is important to note creating a menu item is the only way for a Joomla! user to define a route that leads to a specific component. It is, however, possible to create a route without showing it on the site (in a menu). An often-applied method is to create a menu item in a menu that isn't displayed anywhere. Such a menu is usually called a hidden menu.

The previous paragraph implies that it is not possible to make one component responsible for handling all routes. For example, it is not possible to specify that the URL http://example.com/thealias should display the Content item with the alias thealias, where thealias could be any word. If this needs to be done for a small number of articles, menu items can be created manually for these. Else a routing extension is necessary.

This routing mechanism is therefore not as flexible as users sometimes require. On the other hand, it has one large advantage: it reduces the chance of ambiguous routes (routes that could lead to two different pages). Since the first segment of a route is always a menu item alias, the system immediately knows which component's router must be used to parse it.

Implementation Details

Handling Routes

This section describes the routing implementation. If you are a component developer, see Supporting SEF URLs in your component.

Joomla routes are created and resolved by the JRouter class. This class looks in the component root of the currently active component (specified in the option parameter in the query string) and includes the router.php file in that component's root directory. It then calls one of two functions: one for creating the SEF URL and one for interpreting the SEF URL.

/includes/router.php中 Joomla CMS 會覆寫 JRouter class。在這個檔案中,建立及解析的功能會被覆蓋,並且為 Joomla CMS 解析 URL。

The router.php file in each component (for example, /components/com_content/router.php) should contain the following two functions:

  • ContentBuildRoute - this builds the SEF URL
    • Parameters
      • $query - this is a named array containing the querystring variables
    • Returns: an array of segments where each segment is separated by a '/' when later combined to create the actual URL (the items in the array should not contain '/' characters)
  • ContentParseRoute - this interprets an SEF URL
    • Parameters
      • $segments - this is an array that contains the segments of the URL requested.
    • Returns: a name => value array of the querystring variables that the link maps to

系統 - SEF 外掛

Joomla 的 系統 - SEF 外掛繼承 JPlugin 並且會實行onAfterRender() 功能。在此功能中,回應的內容會使用 JResponse::getBody()來取回並送給瀏覽器。接著回應的內容會搜尋包含有 /index.php... 的連結,並且透過呼叫JRoute::_(url)來取代為正確的 SEF URL。

JRoute builds SEF URLs by instantiating a JRouter object and requesting that it build the correct link from the passed in URL.

處理 SEF URL

預設中會由 JRouterSite 物件 (從 /includes/router.php) 處理 SEF URL, and is called by a call to JApplication::route() in index.php. This call is made on the $app variable which is actually an instance of JSite (from /includes/application.php).

JApplication::route() has a non-destructive result on the $_GET array. That is, JApplication::route() sets variables in $_GET by calling JRequest::set() with the overwrite flag set to false. Thus if a variable name is returned from JRouter::route() that is already in $_GET, it will not put that value into $_GET. 這可以實現客製化路由。

客製化路由

Joomla allows you to create your own routing mechanism. In order to create this mechanism you must have a plugin that overrides the JPlugin::onAfterInitialise() function. This function then parses the URL and creates the needed variables in $_GET before the standard Joomla routing is done.

請見 Creating a System Plugin to augment JRouter ,當中有範例。