Score:0

How can I add a dynamic redirect on a 404 hit?

cn flag

I Created a site and URL that returns a 404 Not Found status on www.xyz.com This creates a loss of link equity to the domain and negatively impacts the ability to compete in organic search results.

I wanted to Redirect the URLs returning a 404 status to the defined closest alternative, or parent category, to reclaim link equity from external domains.

Example: Suppose I am using URL http://www.mysite.com/about/xyz/ and it return 404 so I wanted to create any module or system that smartly redirect to nearest parent path like "http://www.mysite.com/about/"

I am not able to find any specific documentation which would point me in the right direction.

Kevin avatar
in flag
Search 404 module?
manoj kumar Srivastaw avatar
cn flag
@Kevin Thanks for your comment actually it's a nice idea I tried that module but it does not fulfill my Exact requirement.
Score:1
cg flag

Try creating a custom event subscriber. If the page is 404, grab the original URL and redirect accordingly.

To do so, create a new module ("response_test" in this example), with the usual .info.yml and .module file.

Then register your event subscriber in the module's services file... (see EventSubscriber Example for a full example.

./response_test.services.yml

services:
    response_test.response_subscriber:
      class: Drupal\response_test\404RedirectResponseSubscriber
      tags:
        - { name: event_subscriber }

Then create a class to handle the event

./src/EventSubscriber/404RedirectResponseSubscriber.php

<?php 

namespace Drupal\response_test;

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;


class 404RedirectResponseSubscriber implements EventSubscriberInterface {
  
  public function onRespond(FilterResponseEvent $event) {
    if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
      return;
    }
    
    $response = $event->getResponse();
    if ($response->getStatusCode() == 404) {

      // Get the old path somehow
      // @see https://stackoverflow.com/q/11047305
      $oldPath = ''; // Try using $response->getUri() or $response->headers->get('location')

      
      // Manipulate the $oldPath to remove the last part
      // @see https://stackoverflow.com/a/51573313/5771750
      $redirectPath = dirname($oldPath);

      $new_response = new RedirectResponse($redirectPath );

      // Prepare a new Symfony\Component\HttpFoundation\Response and use
      $event->setResponse($new_response);

    }
  }
  
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = array('onRespond');
    return $events;
  }
  
}

Note: This answer is partly based of Clive's answer here: https://drupal.stackexchange.com/a/86622

manoj kumar Srivastaw avatar
cn flag
Thanks @Chris :D really helpful for me.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.