VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: silmoa on October 18, 2021, 04:33:28 AM

Title: 404s and redirect
Post by: silmoa on October 18, 2021, 04:33:28 AM
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 (https://192168ll.one/) 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 (https://10001.dev/) 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 (https://19216801.life/) 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.
Title: Re: 404s and redirect
Post by: nehakakar on August 01, 2023, 11:47:18 AM
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..
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.