There are several ways to link to pages on the internet as well as to content and resources within a Drupal system. Pages, images and media can be linked to by means of absolute URLs, absolute paths, relative paths, node ids, or even special syntax provided by various Drupal modules. All are based in full or in part on the URL used to locate the resource. For our purposes, a URL is typically some version of the format: protocol://domain/path?query_string#fragment_id.
Consider these commonly formatted links that might be embedded in Drupal site content. They could all potentially point to the same page.
...are relative to the root directory. So for example, if "myalias" is located in http://mysite/subfolder/, then the absolute path /myalias will point to http://mysite/myalias and not http://mysite/subfolder/myalias.
...are relative to the current directory. So for example, if "myalias" is located in http://mysite/subfolder/ and the current location is http://mysite/subfolder/, then the relative path myalias will point to http://mysite/subfolder/myalias and not to http://mysite/myalias.
Best coding practices almost always recommends using relative paths so that links don?t break when code is moved between environments. For example, a site may be located on dev.mysite.com for development purposes, and on test.mysite.com for testing purposes and on mysite.com for production. If absolute urls are used in development and carried forward into production, the live site will have links undesirably pointing to resources that now may or may not exist in the development environment. If relative paths are used, the path is relative to the corresponding domain and the link will always point to the correct resource as long as the directory structure is consistent between environments.
General Real World Issues
There are several issues that typically need to be addressed in a real world application where links are concerned.
Content managers may inadvertently enter absolute urls that point to internal content creating the problem described above.
When resources change location any links pointing to those resources must also be changed. For example, say a folder called images is moved from sites/default/files/images to sites/themes/mysite/images. There could potentially be hundreds of links that now need to be changed to point to the new location.
Content managers may manually enter relative paths to resources that don?t exist or did, but no longer exist due to a change in the resource?s name, a change in the resource?s location, or deletion of the resource altogether, resulting in broken links.
A conflicting requirement exists for RSS feed content, html emails, and CDNs in that they require all content resources to be addressed with absolute URLs.
Manually tracking and maintaining site links is possible but time consuming and prone to error. There are several Drupal modules that help automate the process of maintaining links. The Pathologic module is the primary module that deals with the main issues outlined above. The following is a summary list of additional modules currently being used in DOC Drupal sites that affect the way URLS and paths are generated and used.
The core CleanURLs module strips the query string generated by Drupal from the path. So the url http://www.example.com/?q=node/83 becomes http://www.example.com/node/83.
The core Path module lets you optionally creates aliases for your Drupal pages. So the url http://www.example.com/node/83 becomes http://www.example.com/myalias.
The PathAuto module automatically generates a friendly alias based on parameters selected by the user.
The third-party Pathologic module is an input filter that converts and caches all internal paths from either relative paths to fully qualified absolute urls or from relative paths to absolute paths, depending on user selection, upon rendering. It doesn?t permanently change the path in the database.
The WYSIWIG API module is a WSYWIG editor used for entering site content. Replaces the Drupal TinyMCE module. Certain WSYWIG editors can be set to modify and save urls in absolute formats, relative formats, or remain unmodified.
The Linkit module allows content managers to select from a valid list of relative paths that point to internal content and/or to enter a link by means of a form. Replaces the NodePicker module.
The LinkChecker module checks for broken links
Although aliases are preferred for human readability purposes, generating them creates duplicate paths to a single resource which is frowned upon with respect to SEO. The GlobalRedirect module generates a 301 redirect using a single url, preferably the aliased one.