Difference between revisions of "Search Engine Friendly URLs"

From Joomla! Documentation

m (Made miscellaneous improvements)
m (Adjusted parts to include)
Line 1: Line 1:
 
<onlyinclude>
 
<onlyinclude>
 
'''Human readable''' or '''search engine friendly''' [[wikipedia:URL|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 human readable URL's. 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 process of creating and processing human readable URLs is called '''routing'''.
 
'''Human readable''' or '''search engine friendly''' [[wikipedia:URL|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 human readable URL's. 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 process of creating and processing human readable URLs is called '''routing'''.
 
+
</onlyinclude>
 
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.
  
Line 9: Line 9:
  
 
Human readable/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 [[How do you implement Search Engine Friendly URLs (SEF)?]] for more information.
 
Human readable/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 [[How do you implement Search Engine Friendly URLs (SEF)?]] for more information.
</onlyinclude>
+
 
 
=== Joomla Routes ===
 
=== Joomla Routes ===
  

Revision as of 07:14, 9 June 2011

Human readable or search engine friendly 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 human readable URL's. 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 process of creating and processing human readable URLs is called routing.

A good example of routing is the URL to "Welcome to Joomla!" article in the sample data.

  • Without human readable 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 human readable 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 human readable URLs and mod_rewrite on, it's http://www.example.com/the-­news/1­-latest-­news/1-­welcome-­to­-joomla

Human readable/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 How do you implement Search Engine Friendly URLs (SEF)? for more information.

Joomla Routes[edit]

Joomla routes are created by the JRouter class. This class looks in the component root of the component specified in the "option" portion of 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 link and one for interpreting the SEF link.

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.

com_component/router.php

The router.php file 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 link (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

For more information on creating a router, see Supporting SEF URLs in your component.

The SEF Plugin[edit]

The Joomla System SEF plugin inherits JPlugin and overrides 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[edit]

By default the SEF URLs are handled by the 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 $mainframe variable which is actually an instance of JSite (from /includes/application.php).

JApplication::route() has a non-destructive result on the $_GET array. By this I mean that 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 then it will not put that value into $_GET. This allows for custom routing.

Custom Routing[edit]

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.