n How to create a redirection programmatically in Drupal 8 & 9 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

If you want to store redirects in database use the module Redirect. This module provides a user interface to add redirects. The redirects are stored in a content entity, which you can also create programmatically:

use Drupal\redirect\Entity\Redirect;

  Redirect::create([
    'redirect_source' => 'redirects/redirect1',
    'redirect_redirect' => 'internal:/node/1',
    'language' => 'und',
    'status_code' => '301',
  ])->save();

You can do this at the time you create nodes from imported data or in entity hooks when inserting, updating or deleting nodes.

 

Redirection nodes programmatically in Drupal 8 & 9

 

/**
 *  run cron
 */
function MODULE_cron(){
    $nids = \Drupal::entityQuery('node')
        ->condition('type', 'article', '=')
        ->condition('status', 1)
        ->execute();
    foreach ($nids as $nid) {
        $alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$nid);
        $aliasOriginal = str_replace("/blog","blog",$alias);
        $aliasOriginal = str_replace("/web/drupal","",$aliasOriginal);
        Redirect::create([
            'redirect_source' => $aliasOriginal,
            'redirect_redirect' => "internal:/node/".$nid,
            'status_code' => 301,
        ])->save();
    }
}

 

Redirection Terms programmatically in Drupal 8 & 9

 

/**
 *  run cron
 */
function MODULE_cron(){
    $tids = \Drupal::entityQuery('taxonomy_term')
        ->condition('vid', 'tags', '=')
        ->execute();
    foreach ($tids as $tid) {
        $alias = \Drupal::service('path.alias_manager')->getAliasByPath('/taxonomy/term/'.$tid);
        $aliasOriginal = str_replace("tags","",$alias);
        $aliasOriginal = str_replace("/","",$aliasOriginal);
        Redirect::create([
            'redirect_source' => $aliasOriginal,
            'redirect_redirect' => "internal:/taxonomy/term/".$tid,
            'status_code' => 301,
        ])->save();
    }
}

 

More information about module Redirect:

 

Provides the ability to create manual redirects and maintain a canonical URL for all content, redirecting all other requests to that path.

Features

  • Common API for loading, saving, and deleting redirects.
  • Case-insensitive redirect matching with a hook to allow other modules to narrow-down the candidate redirects.
  • Redirect counter and last used timestamp, with automatic cleanup of inactive redirects. (Provided by Redirect Metrics in D8)
  • Integration with Drupal's page cache to optimize redirects and performance.
  • Complete individual redirect access API.
  • Views API integration.
  • D8: Maintaining a canonical path and redirecting alternative URL's like non-aliased path, path without language prefixes and so on (Previously provided by Global redirect)
  • D8: Separate Redirect 404 module that logs aggregated 404 request (can suppress them in the default log) and allows to create redirects from them
  • D8: Separate Redirect Domain module allows wildcard and domain redirects

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook