It's questionable whether having the title in the URL will be of any help in terms of SEO. The alias is usually the SEF version of the title anyways. But to answer your question, existing URLs can be altered by adding custom routing rules. This can be done using a system plugin. The methods used for this are Joomla\CMS\Router\Router::attachBuildRule()
and Joomla\CMS\Router\Router::attachParseRule()
.
There are build rules and parse rules, and they can be attached at different stages (defined by constants in Joomla\CMS\Router\Router
class).
You would register these rules early in the plugin:
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Router\Router;
public function onAfterInitialise()
{
// Get the site router.
$router = CMSApplication::getRouter('site');
// Attch custom rules.
$router->attachBuildRule([$this, 'preprocessBuild'], Router::PROCESS_BEFORE);
$router->attachBuildRule([$this, 'postprocessBuild'], Router::PROCESS_DURING);
$router->attachParseRule([$this, 'postprocessParse'], Router::PROCESS_AFTER);
}
Both build and parse rules are in form of callables which accept an instance of the router and the URI object:
use Joomla\CMS\Router\SiteRouter;
use Joomla\CMS\Uri\Uri;
public function preprocessBuild(SiteRouter $router, Uri $uri)
{
// Your logic here.
}
Build rules are used when building a SEF URL from non-SEF URL. This is where you would append the title by manipulating the URI instance. But before that you would need to determine you are touching the correct URL. You would to do that based on query variables in the $uri
.
Parse rules are used when resolving current SEF URLs to a set of input variables. Each path segment is resolved and then removed from the $uri
. If a segment is remanining, Joomla considers this to be an invalid URL and throws a 404. In your case, you would need to check if the remaining title matches the title of the article with a given ID in the database and remove it if it does.