News:

Looking for documentation? Take a look on our wiki

Main Menu

404s and redirect

Started by silmoa, October 18, 2021, 04:33:28 AM

Previous topic - Next topic

silmoa

Hi,

I reorganized the categories in my shop and replaced them with new ones and all products have been moved to the new categories. After that I added an entry in the Joomla redirect module for all the products so that the "old" links listet at Google 192.168.l.l will be redirected to the new links. After disabling the "404 error handling" in VM it is working fine and the links are being redirected to the new links. So far so good.

But the problem is now, that links to old/sold products that are no longer available in the shop will no longer be automatically 10.0.0.1 redirected to a valid page within the VM shop and getting a 404 error what is bad.

So I am wondering now if there is a way to have both activated? The process should look like this.

1. Not existing link gets 404 error
2. it will then be checked if there is an entry in the Joomla redirect table for this link
3. If this is the case the new link will then be used to redirect the request
4. If no entry is available in the Joomla redirect table the request should then be redirected to the VM shop startpage

I think the logic is not complicated and it could 192.168.0.1 be realized with only a few lines of code and could be very interesting for a lot of people using VM.

Maybe there is somebody who has already realized such a process.

Joomla: 3.9.23
Virtuemart: 3.6.10

Thanks in advance for your help.

nehakakar

Yes, you can achieve the desired behavior of handling 404 errors and using Joomla's redirect table in combination with VirtueMart.
You can implement this functionality by creating a custom plugin or using an existing plugin that allows you to customize the redirection process.
Here are some steps..

  • Create a custom Joomla plugin that triggers on the onAfterRoute event. This event occurs after Joomla has determined which component should handle the current request.
  • In the plugin, check if the current request resulted in a 404 error. You can do this by examining the HTTP response code.
  • If a 404 error is detected, query the Joomla redirect table to check if there is an entry for the requested URL.
    You can use Joomla's database API to perform this query.
  • If there is a matching entry in the Joomla redirect table, redirect the request to the new URL specified in the redirect entry.
    If there is no entry, redirect the request to the VirtueMart shop's start page.
Here is an example code to give you an idea of how the plugin could be implemented...

use Joomla\CMS\Plugin\CMSPlugin;

class plgSystemCustomRedirect extends CMSPlugin
{
    public function onAfterRoute()
    {
        $app = JFactory::getApplication();

        // Check if the current request resulted in a 404 error
        if ($app->isClientError(404)) {
            // Get the requested URL
            $requestedUrl = JUri::current();

            // Query the Joomla redirect table for a matching entry
            $db = JFactory::getDbo();
            $query = $db->getQuery(true)
                ->select('new_url')
                ->from('#__redirect_links')
                ->where('old_url = ' . $db->quote($requestedUrl));
            $db->setQuery($query);
            $newUrl = $db->loadResult();

            // If there is a matching entry, redirect to the new URL
            if ($newUrl) {
                $app->redirect($newUrl);
            } else {
                // If no entry is found, redirect to VirtueMart shop's start page
                $app->redirect('index.php?option=com_virtuemart');
            }
        }
    }
}

This is just a simplified example, and you may need to adapt the code to fit your specific Joomla and VirtueMart versions.