検索エンジンフレンドリーURL
From Joomla! Documentation
検索エンジンフレンドリーURL,人が読みとれるやclean URLsは特定ページへのパスを人と検索エンジン両方に理解できるURLです。Joomla! バージョン1.5以降ではSEF URLを含む全フォーマットでもURLを作成し解析できます。Joomla!の場合、WebサーバのURL書換えに依存しない為、Joomla!はmod_rewriteモジュールを使用しているApache以外のサーバでも作動します。SEF URLは特定の固有パターンがありますがユーザはURL毎に短い説明(エイリアス)を含める事ができます。
内部的にSEF URL(ドメイン名以降)のパスはルートと呼ばれています。SEF URLの作成と解析処理はルーティングと呼ばれ関連するコードはルータと呼ばれています。
ルーティングのよい例がサンプルデータ内の記事"Joomla!へようこそ"です。
- 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は無効
http://www.example.com/index.php/the-news/1-latest-news/1-welcome-to-joomla
- SEF URL,mod_rewrite両方有効
http://www.example.com/the-news/1-latest-news/1-welcome-to-joomla
検索エンジンフレンドリーURLはグローバル設定内の検索エンジンフレンドリーURLで設定できますこのオプションはJoomla!1.6からデフォルトで有効になっています。詳細は検索エンジンフレンドリーURLを有効化を参照して下さい。
FAQs
URL内の数字はなんですか?
古い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!が必要とするパラメータでURLから取得し表示したいページを探します。 (この場合、最初の数字1はカテゴリーのID、二つ目の数字1は、記事のIDです)
URLには index.php
がありませんこのファイルは削除していいですか?
削除してはいけませんURLにはindex.php
が含まれていませんが内部的にmod_rewriteが表示しないで元のパスへリダイレクトしているためです。
エイリアスとは?またどの様に作成されますか?
エイリアスは記事、カテゴリ、セクション、メニューアイテム等のタイトルフィールドの下に表示されています。Joomla!は自動的にエイリアスを作成でき自動作成されたエイリアスはタイトルから始まりすべて小文字に変換されます。スペースやURLで使用できない文字はダッシュに変換されます。
エイリアスを自分で作成したい
Joomla!が作成するエイリアスが気に入らない場合、エイリアスフィールドに好きな値を入れる事ができます。大抵の場合URL内に効果的なキーワードを使用するとSEO対策となるでしょう。記事のタイトルにこれらのキーワードを含みJoomla!で作成したり独自のエイリアスを作る事ができます。
How is Alias used in a URL?
For a menu item, Joomla! uses the alias as the URL slug. Assume that you use the first two SEF URL options and you create a menu item called Products. Your URL would be example.com/products.
Joomla!のルータが正しいページに移動する為、URL内のプライマリーキーを使用しています。上記例のメニューアイテムの種類がカテゴリブログの場合、記事タイトルや続きを読むリンクの3個のパーツ:
- メニューアイテムのURL - example.com/products;
- カテゴリのプライマリーキーとカテゴリのエイリアスを追加 - 32-fruit;
- 記事のプライマリーキーと記事のエイリアスを追加 - 1-apple;
完成したURL: http://example.com/products/32-fruit/1-apple
SEF URLの数字をなくすには?
SEF URLの数字はサイトトラフィック制御するJoomla!ルータが必要です。ルータロジックが安定したらサードパーティ製のシステムプラグインを選択しルータの機能を強化できるでしょう。その時にURLから数字は削除されます。
ルートフォーマットとルーティングの仕組み
このセクションではJoomla!コア(内蔵)のルーティングの仕組みを説明します。ルーティングエクステンションはシステムで作成するルートを変更する事があります。
ルートフォーマット
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.
Limitations
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.
The JRouter class is overridden by the Joomla CMS in /includes/router.php
. In this file the build and parse functions are overridden to properly build and parse the URLs for the Joomla CMS.
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)
- Parameters
- 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
- Parameters
The SEF Plugin
The Joomla System - SEF plugin inherits JPlugin
and implements the onAfterRender()
function. In this function the body of the response that will be sent to the browser is retrieved using JResponse::getBody()
. The body of the response is then searched for links containing /index.php...
and replaces them with a correct SEF URL by calling JRoute::_(url)
.
JRoute builds SEF URLs by instantiating a JRouter
object and requesting that it build the correct link from the passed in URL.
Handling SEF URLs
By default the SEF URLs are handled by the JRouterSite
object (from /includes/router.php
) 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
. This allows for custom routing.
Custom Routing
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.
See Creating a System Plugin to augment JRouter for an example.