Google has indexed an URL on my site like this: https://www.joomla.org/?Itemid=
This results in strange page of course. Try it with your own site!
What would be the best way to redirect this to the Joomla default menu item?
Because Joomla's Redirect Manager did not work and a 301 in the htaccess neither...
My solution is this:
// Joomla System Trigger
public function onAfterRoute()
{
// some basic checks
if ($this->app->isSite() === false) return;
$app = JFactory::getApplication();
$Itemid = $app->input->getInt('Itemid');
// if an Itemid exists return
if ($Itemid) return true;
// get the default menu item id
$menu = JFactory::getApplication()->getMenu();
$menu_default = $menu->getDefault();
$menu_default_id = $menu_default->id;
// route it
$link = 'index.php?Itemid=' . $menu_default_id;
$link = JRoute::_($link);
// redirect to it
$app->redirect($link, 'We had an empty Itemid.');
}
But could / should this be handled differently?
The other methods I tried were as follows:
.htaccess
Redirect 301 /?Itemid= https://www.joomla.org/
And via the Redirect Manager:
The <jdoc:include type="component" />
is empty (of course). That is what I meant by strange.
That's why I want it to redirect to the default menu item.
Btw. Google indexed the page (only) with the Google Links. e.g. Google Nieuws
It's not in my sitemap, not anywhere in the code / database. Screaming Frog SEO spidered it completely.
But ok, I asked Google to remove the URL. We'll see.