Search Engine Friendly URLs: Difference between revisions
From Joomla! Documentation
m Improved naming consistency |
m Adjusted layout |
||
| Line 4: | Line 4: | ||
A good example of routing is the URL to "Welcome to Joomla!" article in the sample data. | A good example of routing is the URL to "Welcome to Joomla!" article in the sample data. | ||
* Without SEF URLs turned on, the URL is < | * Without SEF URLs turned on, the URL is <tt><nowiki>http://www.example.com/index.php?option=com_content&view=article&id=1:welcome-to-joomla&catid=1:latest-news&Itemid=50</nowiki></tt> | ||
* With SEF URLs on and mod_rewrite off, it's < | * With SEF URLs on and mod_rewrite off, it's <tt><nowiki>http://www.example.com/index.php/the-news/1-latest-news/1-welcome-to-joomla</nowiki></tt> | ||
* With both SEF URLs and mod_rewrite on, it's < | * With both SEF URLs and mod_rewrite on, it's <tt><nowiki>http://www.example.com/the-news/1-latest-news/1-welcome-to-joomla</nowiki></tt> | ||
Search engine friendly URLs can be activated by turning on the '''Search Engine Friendly URLs''' option in the ''Global Configuration''. This option is on by default since Joomla! 1.6. See [[Enabling Search Engine Friendly (SEF) URLs]] for more information. | Search engine friendly URLs can be activated by turning on the '''Search Engine Friendly URLs''' option in the ''Global Configuration''. This option is on by default since Joomla! 1.6. See [[Enabling Search Engine Friendly (SEF) URLs]] for more information. | ||
| Line 15: | Line 15: | ||
By comparing the old and the new URL we can see numbers in the old URL, | By comparing the old and the new URL we can see numbers in the old URL, | ||
< | <tt><nowiki>http://www.example.com/index.php?option=com_content&view=article&id=1:welcome-to-joomla&catid=1:latest-news&Itemid=50</nowiki></tt> | ||
but also in the new URL: | but also in the new URL: | ||
< | <tt><nowiki>http://www.example.com/the-news/1-latest-news/1-welcome-to-joomla</nowiki></tt> | ||
These numbers are the parameters that are needed by Joomla! to get the internal URL and show the page you want to see. (In this case, the first numeral one is the ID of the category, the second numeral one is the ID of the article.) | These numbers are the parameters that are needed by Joomla! to get the internal URL and show the page you want to see. (In this case, the first numeral one is the ID of the category, the second numeral one is the ID of the article.) | ||
=== There is no | === There is no <tt>index.php</tt> in the URL anymore. Can I delete the file now? === | ||
No! The URL may doesn't contain the | No! The URL may doesn't contain the <tt>index.php</tt> anymore, but internally the mod_rewrite will only redirect to the original path without showing it to you. | ||
=== What is the Alias value? And how does it get created? === | === What is the [[Alias]] value? And how does it get created? === | ||
Alias is listed beneath the Title field in Articles, Categories, Sections and Menu Items. Joomla! can automatically create the | 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. | ||
=== I want to specify my own value for Alias. === | === I want to specify my own value for Alias. === | ||
| Line 43: | Line 43: | ||
* Plus, the primary key for the Article and the Article alias - 1-apple; | * Plus, the primary key for the Article and the Article alias - 1-apple; | ||
The complete URL is: < | The complete URL is: <tt><nowiki>http://example.com/products/32-fruit/1-apple</nowiki></tt> | ||
=== How can I get rid of the numbers in the SEF URLs? === | === 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. | 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. | ||
== | == Implementation Details == | ||
=== Joomla Routes === | === Joomla Routes === | ||
''This section describes the routing implementation. If you are a component developer, see [[Supporting SEF URLs in your component]].'' | |||
The router.php file in each component (for example, /components/com_content/router.php) should contain the following two functions: | Joomla routes are created and resolved by [[JRouter|the JRouter class]]. This class looks in the component root of the currently active component (specified in the <code>option</code> parameter in the query string) and includes the <tt>router.php</tt> 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. | ||
* ContentBuildRoute - this builds the SEF | |||
The JRouter class is overridden by the Joomla CMS in <tt>/includes/router.php</tt>. In this file the build and parse functions are overridden to properly build and parse the URLs for the Joomla CMS. | |||
The <tt>router.php</tt> file in each component (for example, <tt>/components/com_content/router.php</tt>) should contain the following two functions: | |||
* ContentBuildRoute - this builds the SEF URL | |||
** Parameters | ** Parameters | ||
*** $query - this is a named array containing the querystring variables | *** $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 | ** 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 | * ContentParseRoute - this interprets an SEF URL | ||
** Parameters | ** Parameters | ||
*** $segments - this is an array that contains the segments of the | *** $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 | ** Returns: a name => value array of the querystring variables that the link maps to | ||
=== The SEF Plugin === | === The SEF Plugin === | ||
The Joomla System SEF plugin inherits JPlugin and | The Joomla ''System - SEF'' plugin inherits <code>JPlugin</code> and implements the <code>onAfterRender()</code> function. In this function the body of the response that will be sent to the browser is retrieved using <code>JResponse::getBody()</code>. The body of the response is then searched for links containing <tt>/index.php...</tt> and replaces them with a correct SEF URL by calling <code>JRoute::_(''url'')</code>. | ||
JRoute builds SEF URLs by instantiating a JRouter object and requesting that it build the correct link from the passed in URL. | JRoute builds SEF URLs by instantiating a <code>JRouter</code> object and requesting that it build the correct link from the passed in URL. | ||
=== Handling SEF URLs === | === 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 $ | By default the SEF URLs are handled by the <code>JRouterSite</code> object (from <tt>/includes/router.php</tt>) and is called by a call to <code>JApplication::route()</code> in index.php. This call is made on the <code>$app</code> variable which is actually an instance of <code>JSite</code> (from <tt>/includes/application.php</tt>). | ||
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. | <code>JApplication::route()</code> has a non-destructive result on the <code>$_GET</code> array. That is, <code>JApplication::route()</code> sets variables in <code>$_GET</code> by calling <code>JRequest::set()</code> with the overwrite flag set to false. Thus if a variable name is returned from <code>JRouter::route()</code> that is already in <code>$_GET</code>, it will not put that value into <code>$_GET</code>. This allows for custom routing. | ||
=== 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. | Joomla allows you to create your own routing mechanism. In order to create this mechanism you must have a plugin that overrides the <code>JPlugin::onAfterInitialise()</code> function. This function then parses the URL and creates the needed variables in <code>$_GET</code> before the standard Joomla routing is done. | ||
[[Category:Search Engine Friendly URLs| ]] | [[Category:Search Engine Friendly URLs| ]] | ||
Revision as of 22:25, 20 November 2012
Search engine friendly (SEF), human-readable or clean URLs are URLs that make sense to both humans and search engines because they explain the path to the particular page they point to. Since version 1.5, Joomla! is capable of creating and parsing URLs in any format, including SEF URLs. This does not depend on URL rewriting executed by the web server, so it works even if Joomla! runs a server other than Apache with the mod_rewrite module. The SEF URLs follow a certain fixed pattern, but the user can define a short descriptive text (alias) for each segment of the URL. Internally, creating and processing SEF URLs is called routing, and the relevant code is called a router.
A good example of routing is the URL to "Welcome to Joomla!" article in the sample data.
- Without SEF URLs turned on, the URL is http://www.example.com/index.php?option=com_content&view=article&id=1:welcome-to-joomla&catid=1:latest-news&Itemid=50
- With SEF URLs on and mod_rewrite off, it's http://www.example.com/index.php/the-news/1-latest-news/1-welcome-to-joomla
- With both SEF URLs and mod_rewrite on, it's http://www.example.com/the-news/1-latest-news/1-welcome-to-joomla
Search engine friendly URLs can be activated by turning on the Search Engine Friendly URLs option in the Global Configuration. This option is on by default since Joomla! 1.6. See Enabling Search Engine Friendly (SEF) URLs for more information.
Frequently Asked Questions
What do the numbers in the URL mean?
By comparing the old and the new URL we can see numbers in the old URL,
http://www.example.com/index.php?option=com_content&view=article&id=1:welcome-to-joomla&catid=1:latest-news&Itemid=50
but also in the new URL:
http://www.example.com/the-news/1-latest-news/1-welcome-to-joomla
These numbers are the parameters that are needed by Joomla! to get the internal URL and show the page you want to see. (In this case, the first numeral one is the ID of the category, the second numeral one is the ID of the article.)
There is no index.php in the URL anymore. Can I delete the file now?
No! The URL may doesn't contain the index.php anymore, but internally the mod_rewrite will only redirect to the original path without showing it to you.
What is the Alias value? And how does it get created?
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.
I want to specify my own value for Alias.
If you do not like the alias provided by Joomla!, you can enter a value of your choosing into that field. Many believe using good keywords in your URL helps search engine optimization. You can do so by including those keywords in your title, and allowing Joomla! to create the alias, or by creating the alias yourself.
How is Alias used in a URL?
For a menu item, Joomla! uses the alias as the URL plug. 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! 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.
Implementation Details
Joomla 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.